Jax
Jax

Reputation: 997

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

Is there any way to fix this?

Upvotes: 0

Views: 1771

Answers (2)

angus
angus

Reputation: 690

Change int sd = GridView1.EditIndex; to int sd = GridView1.EditIndex + 1;

A better method would be to use the datakeynames property of the gridview.

Setting datakeyname="id" on the gridview will allow you to retrieve the Id with int Id = Convert.ToInt32(GridView1.DataKeys[Gridview1.EditIndex].Values[0])

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. 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. This would help guard against an injection attack.

Upvotes: 1

Chris Gessler
Chris Gessler

Reputation: 23113

You're using the wrong ID. The EditIndex refers to the ROW number of the item you're editing. You'll have to get the ID from your bound entity and pass that instead.

I'm not entirely certain what you're trying to do with your SQL, but that doesn't really matter. The SQL table has an ID field and it looks like that ID value is bound to your GridView, possibly using a Label. 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.

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();          
    }          
  } 
}

Upvotes: 0

Related Questions