SQL OutParameter Not Working

151 Views Asked by At
private static SqlParameter AddNewParameterToCommand(SqlCommand command,
    string name, object value, bool isOutputParameter)
{       
    SqlParameter parm = new SqlParameter();
    parm.ParameterName = name;
    parm.Value = value;
    command.Parameters.Add(parm);

    if (isOutputParameter == true)
    {
        command.Parameters.Add(new SqlParameter("@parameter"));
    }

    return parm;
}

Here is what I was trying to setup but have been unable to: If the isOutputParameter parameter is true, the new SqlParameter object is set up to accept data back from the database when the command is run.

2

There are 2 best solutions below

1
On BEST ANSWER

You need to set SqlParameter.Direction attribute.

if (isOutputParameter)
   {
    param.Direction=ParameterDirection.Output;
   }
0
On
private static SqlParameter AddNewParameterToCommand(SqlCommand command,
    string name, object value, bool isOutputParameter)
{
    SqlParameter parm = new SqlParameter();
    parm.ParameterName = name;
    parm.Value = value;

    if (isOutputParameter)
    {
        parm.Direction = ParameterDirection.InputOutput;
    }

    command.Parameters.Add(parm);

    return parm;
} 

Ref: SqlParameter.Direction