Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     conn.Open();
     int idx = e.RowIndex;
     GridViewRow row1 = GridView1.Rows[idx];
     string s = row1.Cells[0].Text;
     TextBox name = (TextBox)row1.Cells[1].Controls[0];
     TextBox course = (TextBox)row1.Cells[2].Controls[0];
     string q ="update Student set Name='" +name.Text+"',Course='" +course.Text+"'";
     SqlCommand cmd=new SqlCommand(q,conn);
     cmd.ExecuteNonQuery();
            
     GridView1.EditIndex = -1;           
     conn.Close();
}

when i update a particular row all the rows get updated with the same name.I know i need to have a WHERE keyword in my query but i cant figure out what to write in the where clause?
Posted
Updated 21-Mar-10 22:02pm
v3

First of all, this is a very bad of writing code. having SQL calls in RowUpdate is not a good programming method.

Now coming to your problem:
hemant_chauhan wrote:
I know i need to have a WHERE keyword in my query but i cant figure out what to write in the where clause

Looks like, you are trying to update course of a Student. If one student can have only one course, then, you need to define(or there muse be) a primary key associated with a student. Since primary Key's are unique, you can retrieve those before binding it to Gridview and extract that while updating as you did for name and course, then use that key in your where clause.

If student can have multiple courses, then you need to have a little normalized Database where again using primary keys you can update it.
 
Share this answer
 
which method can i apply then if we dont want to use sql to update database???
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900