Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why the data are not updating in table.

My cs coading is given below

C#
void submit(Object s, EventArgs e)
           {
           con.Open();
           com = con.CreateCommand();
           if (con != null)
               {
               com.CommandText = "UPDATE joining SET aname = '" + txtName.Text.Trim() + "',fathname='" + txtFather.Text.Trim() + "' where refno='" + txtRefNo.Text.Trim() + "'";
               try
                   {
                   com.ExecuteNonQuery();
                   System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
                   lbl1.ForeColor = System.Drawing.Color.Yellow;
                   lbl1.BackColor = System.Drawing.Color.Blue;
                   lbl1.Text = "Your record UPDATED sucessfully";
                   ph1.Controls.Add(lbl1);
                   }
               catch (Exception ex)
                   {
                   Response.Write(ex.Message);
                   }
               }
           con.Close();
           }


[edit]Code block added, code formatted, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 20-Jan-12 21:56pm
v3
Comments
Kim Togo 21-Jan-12 3:52am    
Repost - See http://www.codeproject.com/Questions/317868/Why-the-data-are-not-updating-in-table
Sergey Alexandrovich Kryukov 21-Jan-12 4:17am    
Time to report it... :-)
--SA

1 solution

Probably, it is down to the contents of your text boxes. Do not 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:
C#
com.CommandText = "UPDATE joining SET aname = @NM,fathname=@FN where refno=@RN";
com.Parameters.AddWithValue("@NM", txtName.Text.Trim());
com.Parameters.AddWithValue("@FN", txtFather.Text.Trim());
com.Parameters.AddWithValue("@RN", txtRefNo.Text.Trim());
Not only will your code be better protected, but your problem may well go away as well.
 
Share this answer
 
Comments
Janardan Pandey 21-Jan-12 5:20am    
but this is not working
OriginalGriff 21-Jan-12 5:21am    
So what is, or isn't happening?
Janardan Pandey 21-Jan-12 5:22am    
the data which i want to update in the table not going.
OriginalGriff 21-Jan-12 5:36am    
Are you getting any messages?
Janardan Pandey 21-Jan-12 6:01am    
no i am not getting any error message.Only showing your record updated.But when i am going to see in the table records is not updated.

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