Click here to Skip to main content
15,886,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      
        cn.Open();
        int customerid = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
       String customername =(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
       String email = (((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
       String amount = (((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
        SqlCommand cmd = new SqlCommand("update customer set customername='" + TextBox2.Text + "' where customerid='" + TextBox1.Text + "' ",cn);
        cmd.ExecuteNonQuery();
        cn.Close();
        Console.WriteLine("update end");
        BindGridview();
        clear();
          
     }


i was updating My grid view control update button but it is not update please help me..

What I have tried:

i was updating My grid view control update button but it is not update please help me..
Posted
Updated 5-Jun-18 2:59am
v2
Comments
Herman<T>.Instance 5-Jun-18 9:02am    
How is your GridView control in the ASPX file set up.
Are you using DataKeynames for your customerId?
Try catch for any error?
What is the ID of the TextBox that holds the customerID?
What is the ID of the TextBox that holds the CustomerName?
Have you set the EditIndex and the SelectedIndex when setting the GridView in the RowEditing event?

Questions end with an ? So maybe modify your question
Richard Deeming 6-Jun-18 11:32am    
SqlCommand cmd = new SqlCommand("update customer set customername='" + TextBox2.Text + "' where customerid='" + TextBox1.Text + "' ",cn);


Not like that!

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

using (SqlCommand cmd = new SqlCommand("update customer set customername = @customername where customerid = @customerid",cn))
{
    cmd.Parameters.AddWithValue("@customername", TextBox2.Text);
    cmd.Parameters.AddWithValue("@customerid", TextBox1.Text);
    
    cmd.ExecuteNonQuery();
}

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