简体   繁体   English

从数据库获取一个单元格的值到一个字符串中:我不能仅从下一行就从当前行中获取它

[英]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

  • If I click edit and update on the first row I get this : 如果单击第一行上的“编辑并更新”,则会显示以下内容:

    Object reference not set to an instance of an object. 你调用的对象是空的。

  • If I click edit and update on the second row , I get the value from the first row. 如果单击第二行上的编辑和更新,则从第一行获取值。
Is there any way to fix this? 有没有什么办法解决这一问题?

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.

相关问题 gridview:如何从当前编辑的行的数据库单元格获取值? - gridview: How to get a value from database cell of the current edited row? 根据刚在单元格中输入的值,从数据库的C#datagridview中的当前行填充数据 - Fill data in current row in a C# datagridview from a database according to the value just entered in a cell 如何从具有一行的数据集中的标题名称中获取单元格值 - how can I get a cell value from a header name in a dataset with one row 如何从数据网格单元格中获取字符串值? - How can i get a string value from a datagrid cell? 我无法从gridview的页脚行中获取文本框的值 - I can't get the value of textbox from the footer row of gridview 如何使用 SQLite 从数据库表中仅获取一个字段? - How can I get just one field from a database table with SQLite? 登录 - 如何从数据库单元格中获取值到字符串? - Login - How to get a value from database cell into a string? 如何从Gridview中的上一行到当前行获取特定单元格值? Winforms Devexpress - How to get Particular cell value from Previous row to Current Row in Gridview ?? Winforms Devexpress 无法从下拉框到数据库获取价值 - Can't get value from dropdownbox to database 无法从继承中获取字符串值 - can't get string value from inheritance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM