Search code examples
sqlsql-servervb.netstored-proceduresout-parameters

How can I use the OUT parameter from a SQL Server stored procedure in a vb.net code


I have a SQL Server stored procedure that takes an IN parameter and an OUT parameter:

create PROCEDURE  [dbo].[GetServiceByKeyword] @keyword nvarchar(30), @Service INT OUT 
AS 
BEGIN   
    SET NOCOUNT ON;

    select @service = Service 
    from Keyword_Service 
    where Keyword = @keyword 
END

I'm calling it in vb.net like this:

If cn.State = ConnectionState.Closed Then cn.Open()

cm = New SqlCommand("dbo.getservicebykeyword", cn)
cm.CommandType = Data.CommandType.StoredProcedure

cm.Parameters.AddWithValue("@keyword", id)

Dim Srvce As New SqlParameter("@Service", Data.SqlDbType.Int)
Srvce.Direction = Data.ParameterDirection.Output
cm.Parameters.Add(Srvce)

How can I use the output from this stored procedure? (Srvce)

Its the first time I use OUT parameters and I wanna transform the result to String to be able to use it in the code.

Any help would be appreciated


Solution

  • after calling cm.Execute(), you can get the value using:

    dim result=cm.Parameters("@Service").Value
    

    hope it will help you

    regards