Click here to Skip to main content
15,890,741 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)
      {
          int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

          GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
          Label lblID = (Label)row.FindControl("lblID");

          TextBox textName = (TextBox)row.Cells[0].Controls[0];
          TextBox textadd = (TextBox)row.Cells[1].Controls[0];
         // TextBox textc = (TextBox)row.Cells[2].Controls[0];



          GridView1.EditIndex = -1;
          GridView1.DataBind();
          conn.Open();

          SqlCommand cmd = new SqlCommand("update empdet set name='" + textName.Text + "',salary='" + textadd.Text + "'where id='" + id + "'", conn);


          cmd.ExecuteNonQuery();

          conn.Close();
          gvbind();
      }


What I have tried:

i tried to edit the name . but it display an error messge
"Conversion failed when converting the varchar value 'divya' to data type int.
what to do?
Posted
Updated 30-Jan-18 21:02pm
Comments
Mehdi Gholam 31-Jan-18 2:39am    
'divya' is not a number it is a string, so you can't convert it.
[no name] 31-Jan-18 2:49am    
Divya is not an integer represented in string format hence you can't convert.
CPallini 31-Jan-18 3:07am    
How could you solve? I don't know how to. I mean 'divya' is not a number to me. Why the code is expected to convert it to a number?
Laxmidhar tatwa technologies 31-Jan-18 5:34am    
Pl aware us the value of all the textbox and the datatypes of the table fields

1 solution

Stop doing that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And when you have fixed that through your whole app, you will find that your problem has probably disappeared.
 
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