如何使用存储过程更新网格视图中的数据?

时间:2015-03-24 22:55:04

标签: c# asp.net gridview

我尝试使用命令行在网格视图中更新数据,但我得到的错误如下:

  

程序或功能' UpdateState'期望参数' @StateName',这是未提供的。

存储过程:

CREATE PROCEDURE UpdateState
    @StateName varchar(30)
AS
    update tblState 
    set StateName = @StateName 

Aspx页面:

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 {
       GridView1.EditIndex = e.NewEditIndex;
       FillStates();
 }

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
       int Sid = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
       TextBox StateName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
       p.Sid = Sid;
       p.StateName = StateName.Text;
       p.UpdateState(p);
       GridView1.EditIndex = -1;
       FillStates();
}  

来源:

public void UpdateState(Property p)
{
        cmd = new SqlCommand("UpdateState", con);
        cmd.Parameters.AddWithValue("@Sid", p.Sid);
        cmd.Parameters.AddWithValue("@StateName", p.StateName);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
}

1 个答案:

答案 0 :(得分:0)

添加命令类型

    cmd = new SqlCommand("UpdateState", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Sid", p.Sid);
    cmd.Parameters.AddWithValue("@StateName", p.StateName);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();