gridview 删除行时出现指定的参数已超出有效值的范围问题
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){
string sqlconstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(sqlconstr);
sqlconn.Open();
//删除行处理
String sql = "delete from consumer where name=@name" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句
SqlCommand Comm = new SqlCommand(sql, sqlconn);
// Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
Comm.ExecuteNonQuery();
sqlconn.Close();
sqlconn = null;
Comm = null;
GridView1.EditIndex = -1;
bindgrid();
}
总是显示:异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
但 在编辑更新中相同代码((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text却没有错,并没有出现什么超出有效范围,这是什么情况?求大神帮忙!感谢! GridView --------------------编程问答-------------------- 求大神快速回复 --------------------编程问答-------------------- 这个要看你的界面控件的布局顺序,你写的Cells[0],对于删除按钮不一定就是0. --------------------编程问答-------------------- String sql = "delete from consumer where name=@name" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句
删除
+ ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句
--------------------编程问答--------------------
Cells[]这个应该是列吧,顺序下标都是都是从0开始的吧。
我的删除按钮就在编辑(编辑、更新、取消)按钮后。更新操作中也是这么写的都没有错 --------------------编程问答--------------------
没看出区别啊??? --------------------编程问答--------------------
不明白 --------------------编程问答-------------------- @name 这里定义的变量。
// Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
这里又注释了,搞不懂你。 --------------------编程问答-------------------- 新手完全看不懂,
为什么我这边只有DataGridView?
如果他们功能差不多的话可以SelectedRows[]取指定列的值...
请大神解惑 --------------------编程问答--------------------
为什么要集体处理,而不是每个控件单独触发呢? --------------------编程问答--------------------
你那是winform,他这是asp.net。
--------------------编程问答--------------------
哦
谢谢哦 --------------------编程问答--------------------
@name 这里定义的变量。
// Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
这里又注释了,搞不懂你。
哦,忘了删除@name,
String sql = "delete from consumer where name=" + GridView1.DataKeys[e.RowIndex].Value.ToString()+"";
这样,还是一样的错误。注释是因为下面这样写过
String sql = "delete from consumer where name=@name";
Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
还是一样的错误。
补充:.NET技术 , C#