Skip to content Skip to sidebar Skip to footer

Run Sql Stored Procedure And Return Print Message Vb.net

I have a stored procedure with 2 parameters and which returns result is a PRINT message. I can pass the parameters in and run the stored procedure but I can't figure out how to ret

Solution 1:

You can retrieve warnings and informational messages from a SQL Server data source using the InfoMessage event of the SqlConnection object.

This has been answered in a similar question: Get the value of print statement of sql server in vb.net by using oledb

Solution 2:

Thanks! I did some more digging using your link and I've got it working.

Code amended below if anyone else needs an example.

Imports System.Data.Sql
Imports System.Data.SqlClient

PublicClass Form1

Dim SQL AsNew SQLControl
Dim cmd AsNew SqlCommand

PrivateSub RunSql_Click(sender As System.Object, e As System.EventArgs) Handles RunSql.Click

TryIf SQL.HasConnection = TrueThen
        SQL.SQLCon.Open()
        cmd.Connection = SQL.SQLCon
        cmd.CommandText = "Exec stored procedure " & param1.Text & "," & param2.Text
        cmd.ExecuteNonQuery()

        AddHandler SQL.SQLCon.InfoMessage, New SqlInfoMessageEventHandler(AddressOf OnInfoMessage)

    EndIfCatch ex As Exception
    MsgBox(ex.Message)
Finally
    SQL.SQLCon.Close()
EndTryEndSubPrivateSharedSub OnInfoMessage(sender AsObject, args As SqlInfoMessageEventArgs)
    Dim err As SqlError
    ForEach err In args.Errors
        MsgBox(err.Message)
    NextEndSubEndClass

Thanks again, Lewis

Post a Comment for "Run Sql Stored Procedure And Return Print Message Vb.net"