[英]Getting a value of a cell from database into a string: I can't get it from the current row just from the next one
The problem is I can access the value from a certain row only from the next row. 问题是我只能从下一行访问某一行的值。 Here is the code:
这是代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int s = GridView1.EditIndex;
string constr = "con string";
string statment = "Select Name from tb1 Where [ID] = " + s;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand comm = new SqlCommand(statment, con))
{
con.Open();
Label2.Text = comm.ExecuteScalar().ToString();
con.Close();
}
}
}
For example: 例如:
ID Name
Edit Delete Select 1 JOhn
Edit Delete Select 2 SMith
Object reference not set to an instance of an object.
你调用的对象是空的。
Change int sd = GridView1.EditIndex;
更改
int sd = GridView1.EditIndex;
to int sd = GridView1.EditIndex + 1;
到
int sd = GridView1.EditIndex + 1;
A better method would be to use the datakeynames property of the gridview. 更好的方法是使用gridview的datakeynames属性。
Setting datakeyname="id"
on the gridview will allow you to retrieve the Id with int Id = Convert.ToInt32(GridView1.DataKeys[Gridview1.EditIndex].Values[0])
在gridview上设置
datakeyname="id"
将允许您使用int Id = Convert.ToInt32(GridView1.DataKeys[Gridview1.EditIndex].Values[0])
检索ID。
This will use less overhead than creating an object and setting it to a control in the row containing the id to retrieve it's value. 与创建对象并将其设置为包含ID的行中的控件以获取其值相比,这将减少开销。 See link for further reading on implementing datakeynames.
有关实现数据密钥名的更多信息,请参见链接 。
I would also recommend using a System.Data.SqlClient.SqlCommand
for the SQL method and adding the Id via a parameter. 我还建议对SQL方法使用
System.Data.SqlClient.SqlCommand
并通过参数添加Id。 This would help guard against an injection attack. 这将有助于防止注入攻击。
You're using the wrong ID. 您使用了错误的ID。 The EditIndex refers to the ROW number of the item you're editing.
EditIndex引用您正在编辑的项目的ROW号。 You'll have to get the ID from your bound entity and pass that instead.
您必须从绑定的实体获取ID,然后传递该ID。
I'm not entirely certain what you're trying to do with your SQL, but that doesn't really matter. 我不确定您要使用SQL做什么,但这并不重要。 The SQL table has an ID field and it looks like that ID value is bound to your GridView, possibly using a Label.
SQL表具有一个ID字段,看起来该ID值可能已使用Label绑定到了GridView。 You'll have to get that ID by using the RowIndex to pass into your SQL statement because the ID in your SQL table may not necessary line up to the row index.
您必须使用RowIndex传递到您的SQL语句中,以获取该ID,因为SQL表中的ID可能不必与行索引对齐。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// get the row being updated
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
// use the FindControl method to retrieve the control holding the ID
Label lbl = (Label)row.FindControl("lblid");
// you can find other controls as needed using the same method
TextBox tb1 = (TextBox)row.FindControl("textbox1");
TextBox tb2 = (TextBox)row.FindControl("textbox2");
// perform the SQL update, or whatever using the ID (here's your original code)
string constr = "con string";
string statment = "Select Name from tb1 Where [ID] = " + lbl.Text;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand comm = new SqlCommand(statment, con))
{
con.Open();
Label2.Text = comm.ExecuteScalar().ToString();
con.Close();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.