Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all!!

i Want to edit grid view data using ADO.net. I used this code
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       objconnection.Open();
       string id= GridView1.DataKeys[e.RowIndex ].Value.ToString();
       GridViewRow row = GridView1.Rows[e.RowIndex];

       TextBox txtUsername1 = (TextBox)row.FindControl("txtUsername");
       TextBox txtPassword1 = (TextBox)row.FindControl("txtPassword");
       TextBox txtStatus1 = (TextBox)row.FindControl("txtStatus");

      objcommand= new SqlCommand ("update Def set username='" + txtUsername1.Text + "' , password='" + txtPassword1.Text + "' , status='" + txtStatus1.Text + "' where username='"+id+"'",objconnection );
      objcommand.ExecuteNonQuery();
      GridView1.EditIndex = -1;
      databind();


   }



it's not changing the data. when i used breakpoint i found that txtUsername1 has the same text that of txtUsername which i used in the item template therefore i am unable to change data.

the problem is that i am not able to access the textbox text that i have used in edittemplate field.
Posted
Updated 17-Nov-11 21:03pm
v2
Comments
Mehdi Gholam 18-Nov-11 3:03am    
EDIT -> fixed formatting
Sergey Alexandrovich Kryukov 18-Nov-11 3:04am    
Just a note: ADO.NET is not used to edit anything on the UI. I understand this maybe just words, but it's always good to use accurate wording.
--SA
Al Moje 18-Nov-11 3:21am    
I agree with SA.
I think you did not consider to create an '<asp:UpdatePanel>' with UpdateMode="Conditional". Then in your code behind update such panel before geting the value of such text to save...

1 solution

below code may help u dear....


C#
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Button btn = (Button)row.FindControl("Edit");
            Label Id = (Label)row.FindControl("lbl_EmployeeId");
             TextBox employeename = (TextBox)row.FindControl("txt_Ename");
            TextBox Workstation = (TextBox)row.FindControl("txt_Workstation");
            sq = "update Employee  set Ename='+employeename & Workstation ='+Workstation where EmployeeID='+id";
            SqlCommand cm = new SqlCommand(sq, con);
            cm.ExecuteNonQuery();
bind();
 
Share this answer
 
v2
Comments
uspatel 18-Nov-11 7:18am    
pre tag added

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