Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
GridViewRow row = (GridViewRow)ResultGridView.Rows[e.RowIndex];
        Label lbl = (Label)row.FindControl("lblid");
        TetBox txextname = (TextBox)row.FindControl("textbox1");
        TextBox textmarks = (TextBox)row.FindControl("textbox2");
        ResultGridView.EditIndex = -1;
                conn.Open();

               OleDbCommand cmd = new OleDbCommand("update db1 set Names='" + textname.Text + "' , Number='" + textmarks.Text + "' where Id='" + lbl.Text + "'",conn);
                //.CommandText = "Update StudentRecord set Name='" + txtname.Text + "',ClassName='" + txtclassname.Text + "',RollNo='" + txtrollno.Text + "',EmailId='" + txtemailid.Text + "' where StId='" + lblstid.Text + "'";

                cmd.CommandType = CommandType.Text;
               OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                cmd.CommandType = CommandType.Text;

        cmd.ExecuteNonQuery();

       
        conn.Close();
        bind();
Posted
Updated 21-Jul-14 21:09pm
v2
Comments
Maarten Kools 22-Jul-14 3:26am    
I suggest to strip the statement until it works,and from there try to determine what exactly is causing the problem. For now, I suggest instead of building the query the way you're doing now to use prepared statements[^]. This has a couple of advantages, one of them is security and the other is that you don't have to bother with determining datatypes and escaping your input. If I had to take a guess at why the update statement fails, I would think it's because the input has a quote in it. Again, using prepared statements will fix this.

1 solution

Concatenating the SQL query with its values is a very good idea to provoke SQL Injection Attacks.
But using parameterized queries instead, such problems like yours are also solved. - I guess you get the error with names ike O'Connor?
 
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