Stored procedure's output parameters not updating

847 views Asked by At

I have created a linq2sql project in which I have an extension method for calling SPs. This extension method has few features-

  • It can call SP with Table Valued Parameters.
  • It can accepts both input and output parameters
  • It will handle multiple result set

Extension Method -

public partial class TestLinq2SqlDataContext : DataContext
{
public IMultipleResults ExceuteProcedure(string spName, IEnumerable<SqlParameter> parameters, SqlConnection sqlConnection, out SqlDataReader reader)
{
    reader = null;
    try
    {
        sqlConnection.Open();
        var cmd = new SqlCommand
            {
                Connection = sqlConnection,
                CommandText = spName,
                CommandType = CommandType.StoredProcedure
            };

        cmd.Parameters.AddRange(parameters.ToArray());
        reader = cmd.ExecuteReader();
        return Translate(reader);
    }
    catch (Exception)
    {
    }
    return null;
}
}

I am calling below SP -

CREATE PROCEDURE USP_OutPutParameterCheck(
   @Id int OUTPUT,
   @Name nvarchar(50) OUTPUT)
AS 
BEGIN   
    SET @Id = 12 SET @Name = N'NameSet for OutputParameter'
END

My C# code is

public static void Main(){
context = new TestLinq2SqlDataContext();

#region USP_OutPutParameterCheck

var connection1 = context.Connection as SqlConnection;
SqlDataReader dataReader1;
var outParam1 = new SqlParameter
{
    Direction = ParameterDirection.Output,
    Value = "Abc",
    DbType = DbType.String,
    ParameterName = "@Name"
};
var outParam2 = new SqlParameter
{
    Direction = ParameterDirection.Output,
    Value = 1,
    DbType = DbType.Int32,
    ParameterName = "@Id"
};
var parameters11 = new[]
    {
        outParam1,
        outParam2
    };
var data21 = context.ExceuteProcedure("USP_OutPutParameterCheck", parameters11, connection1, out dataReader1);
}

Now When I check the values of output parameters in debug mode I am getting the @Id's value perfect but for @Name parameter I'm only getting 'N' value instead of 'NameSet for OutputParameter'

Can anyone help me out where I am going wrong in this?

Thanks

UPDATE :

Adding Screenshot when seeing the values of parameters in debug mode - debug mode

1

There are 1 answers

0
Gabor Rajczi On BEST ANSWER

I think You must specifcy the Size of the outParam1.

See: https://msdn.microsoft.com/en-us/library/system.data.common.dbparameter.size(v=vs.110).aspx

For bidirectional and output parameters, and return values, you must set the value of Size. This is not required for input parameters, and if not explicitly set, the value is inferred from the actual size of the specified parameter when a parameterized statement is executed.