Search code examples
c#sqlsqlparameter

SQL OutParameter Not Working


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.


Solution

  • You need to set SqlParameter.Direction attribute.

    if (isOutputParameter)
       {
        param.Direction=ParameterDirection.Output;
       }