-2

我尝试通过以下代码批准文件

if (e.CommandName == "_Approve")
        {
            //using (SqlConnection con = DataAccess.GetConnected())
            using (SqlConnection con = new 
          SqlConnection(ConfigurationManager.ConnectionStrings["mydms"]
           .ConnectionString))
            {
                try
                {
                    con.Open();
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
                   ((Control)e.CommandSource).NamingContainer;
                    Button Prove_Button = (Button)row.FindControl("BtnApprove");
                    SqlCommand cmd = new SqlCommand("approve", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID", UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveID", ApproveID));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch
                {
                    apfi.Text = "Not Approve";



                }
                finally
                {
                    con.Close();
                }
            }
        }


        else if (e.CommandName == "_Reject")
        {
            using (SqlConnection con = new 
          SqlConnection(ConfigurationManager.ConnectionStrings
        ["mydms"].ConnectionString))
            {
                try
                {
                    con.Open();
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
                    ((Control)e.CommandSource).NamingContainer;
                    LinkButton Prove_Button = (LinkButton)row.FindControl("Button1");
                    SqlCommand cmd = new SqlCommand("sprejectapprove", con);

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID",UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveID", ApproveID));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch 
                {
                    apfi.Text = "Rejct";
                }
                finally
                {
                    con.Close();
                }
            }
        }

并批准存储程序是

 @UserID int,
  @DocID int,
 @ApproveID int
as
insert into Approval(UserID,DocID,ApproveID)
VALUES(@UserID,@DocID,@ApproveID)

并且在 sql 中有一个批准类型表,其中有列批准 ID 和批准类型和批准类型有 3 种类型 1 是批准 2 拒绝和 3 是待定

并且在批准表中有 userid,docid,approveid

所以当我调试代码时,它在批准部分捕获时给了我错误

过程或函数“批准”需要参数“@ApproveID”,但未提供该参数。任何帮助,谢谢

4

1 回答 1

4

I'm guessing that ApproveID has a value of null. Due to some craziness of ADO.NET, null parameters are not sent. You need:

cmd.Parameters.Add(new SqlParameter("@ApproveID", (object)ApproveID ?? DBNull.Value));

or simpler:

cmd.Parameters.Add("ApproveID", (object)ApproveID ?? DBNull.Value);

Or even easier yet, with "dapper" it is much easier to get it right:

con.Execute("approve", new {
    UserID, DocID, ApproveID
}, commandType: CommandType.StoredProcedure);
于 2013-10-01T13:20:30.977 回答