[英]ASP.NET Getting value from gridview
这是我的代码
protected void GridView1_DeletingRow(object sender, EventArgs e)
{
Functions con = new Functions();
SqlConnection con1 = con.get();
con1.Open();
TextBox1.Visible = true;
Button1.Visible = true;
string z = TextBox1.Text;
GridView1.EnableViewState = true;
string name = GridView1.SelectedRow.Cells[1].Text;
SqlCommand com3 = new SqlCommand("select id from products where product
= '" + name + "' ", con1);
SqlDataReader read1 = com3.ExecuteReader();
read1.Read();
string pro = read1["id"].ToString();
SqlCommand com = new SqlCommand("UPDATE CartItems SET quantity = '" + z
+ "' where productid = '" + pro + "' ", con1);
}
错误:
Line 92: string name = GridView1.SelectedRow.Cells[1].Text;
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
到底是什么错误? ,我该如何解决?
您应该将EventArgs
参数更改为更特定的GridViewDeleteEventArgs
类型,并使用该类型来查找要删除的行:
protected void GridView1_DeletingRow(object sender, GridViewDeleteEventArgs e)
{
// ...your code
string name = GridView1.Rows[e.RowIndex].Cells[1].Text;
// ...the rest of your code
NullReferenceException的最可能原因是因为在激发RowDeleting事件时未设置SelectedRow属性。
注意:如果网格只有一列,“ Cells[1].Text
”也有可能抛出该异常。 您应该阅读这篇文章中的建议, 该建议可以帮助您调试NullReferenceExceptions。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.