The stored procedure returns null as the output parameter

I have a stored procedure that works fine in the MS SQL management studio.

When I try to use it in VS lines, it turns out fine, but the value of the output parameters is NULL .

 SqlCommand cmd = new SqlCommand("proc_name", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@p_SomeVal", SqlDbType.Int)); cmd.Parameters["@p_SomeVal"].Direction = ParameterDirection.Output; rdr = cmd.ExecuteReader(); //...process rows... if (cmd.Parameters["@p_SomeVal"].Value != null) SomeVal = (int)cmd.Parameters["@p_SomeVal"].Value; 

cmd.ExecuteNonQuery (); It has the same result.

 USE [db_name] GO DECLARE @return_value int, @p_SomeValue int EXEC @return_value = [dbo].[Proc_name] @p_InputVal = N'aa', @p_SomeValue = @p_SomeValue OUTPUT SELECT @p_SomeValue as N'p_SomeValue' SELECT 'Return Value' = @return_value GO 
+7
c # sql stored-procedures
source share
1 answer
 SqlCommand cmd = new SqlCommand("proc_name", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@p_SomeVal", SqlDbType.Int)); cmd.Parameters["@p_SomeVal"].Direction = ParameterDirection.Output; rdr = cmd.ExecuteReader(); //...process rows... rdr.Close(); if (cmd.Parameters["@p_SomeVal"].Value != null) SomeVal = (int)cmd.Parameters["@p_SomeVal"].Value; 

After processing the strings, I added rdr.Close(); and worked fine.

+12
source share

All Articles