使用varchar作为输出参数调用存储过程

时间:2014-04-22 12:48:44

标签: asp.net sql stored-procedures

我想将存储过程的输出参数分配给会话变量。基本上,我希望调用一个具有varchar out参数的存储过程。存储过程如下:

    Alter proc spCheckTaskPerformed
    @TaskId int,
    @Email varchar(100),
    @Status varchar(100) output
    as
    begin
    Select @Status = Status from tblTaskPerformed
    where [Email] = @Email and [TaskId]=@TaskId
    end

我的代码如下:

      private void CheckTaskPerformed()
{
    string CS = ConfigurationManager.ConnectionStrings["EasyRozMoney_ConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("spCheckTaskPerformed", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@TaskId", lblTaskId.Text);
        cmd.Parameters.AddWithValue("@Email", Session["Email"].ToString());

        SqlParameter outputParameter = new SqlParameter();
        outputParameter.ParameterName = "@Status";
        outputParameter.SqlDbType = System.Data.SqlDbType.VarChar;
        outputParameter.Direction = System.Data.ParameterDirection.Output;
        cmd.Parameters.Add(outputParameter);

        cmd.ExecuteNonQuery();

        string Status = outputParameter.Value.ToString();
        lblStatus.Text = "This task has " + Status;
    }
}

但是我收到一条错误消息 - String [2]:Size属性在下面的行中的大小为0。

    cmd.ExecuteNonQuery();

1 个答案:

答案 0 :(得分:0)

您必须将值发送到状态或指定空输入:

 @Status varchar(100) = null output