Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

How to display custom error message from SQL server

$
0
0

I need some help displaying a message form SQL server 2008R to my web application on button click. I am using Visual Studio 2010 and  using vb for the web application.  

My stored procedure is:

 

ALTER PROCEDURE [dbo].[SP_AR_ERROR]

AS

BEGIN

declare @Error varchar(255);

 

WITH CTE AS(

select LEFT ( gl_account,8)  as field1 from AR_Import

except

select gl_account as field2  from GL_CHART_ACCTS)

 

select @Error = STUFF ((select ', ' +field1

                                                      from CTE

                                                      for XML PATH('')

                                                      ), 1, 1, 'There are errors in the Speradsheet');

if LEN(@ERROR) > 0 RAISERROR (@Error, 10,1)

      end

that returns the following level 10 message: There are errors in : 12345-75, 56789-00, 77777-80

 

I need to get the above message and dislay it to the user. What I have so far is not working;

 

 

Imports System.Data.SqlClient

 

Public Class WebForm1

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

    End Sub

 

 

 

 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Try

            Dim cn As New SqlConnection("Server=NAME;Database=DB;User Id=MyUSERID;Password=MyPassword")

            AddHandler cn.InfoMessage, AddressOf cn_InfoMessage

 

            cn.Open()

            Dim cmd As New SqlCommand("SP_AR_ERROR")

            cmd.CommandType = CommandType.StoredProcedure

            cmd.Connection = cn

 

            cmd.Parameters.Add(New SqlParameter("@Error", SqlDbType.VarChar, 255))

 

            Label1.Text = cmd.Parameters("@Error").Value

 

            Dim myReader As SqlDataReader = cmd.ExecuteReader()

            Dim RecordCount As Integer = 0

 

            Do

                Do While myReader.Read()

                    Debug.WriteLine("Foun " & myReader(0))

                Loop

            Loop While myReader.NextResult()

 

            myReader.Close()

 

        Catch SqlEx As SqlException

            Dim myError As SqlError

            Debug.WriteLine("Errors Count:" & SqlEx.Errors.Count)

            For Each myError In SqlEx.Errors

                Debug.WriteLine(myError.Number & " - " & myError.Message)

            Next

        End Try

    End Sub

 

    Public Sub cn_InfoMessage(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlInfoMessageEventArgs)

        Debug.WriteLine("info message event: " & e.Message)

    End Sub

 

End Class

 

 Thank you for your help!

 


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>