Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my aplication has updatepage.aspx which updates database tables .. the problem is when i click the button it takes the old values .. and no error ,, here is my code .. hope to find solution from masters =]

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       prepareConnection();
       //Update News
       if (_CatID == "1")
       {

           if (FileUpload1.HasFile)
           {
               prepareConnection();
               string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
               //save file to disk

               FileUpload1.SaveAs(Server.MapPath("~/ArticleImages/News/" + FileName));
               _command.CommandText = "UPDATE News SET Title=" + "N'" + titleTextBox.Text + "'" + "," + "Contect=" + "N'" + CKEditor1.Text + "'" + ",img=@FilePath WHERE ID='" + Convert.ToInt16(lblID.Text) + "';";
               _command.Parameters.AddWithValue("FilePath", "~/ArticleImages/News/" + FileName);
               try
               {
                   _command.ExecuteNonQuery();
               }
               catch (Exception ex)
               {
                   Response.Write(ex.Message);
               }

           }
           else
           {
               prepareConnection();
               _command.CommandText = "UPDATE News SET Title=" + "N'" + titleTextBox.Text + "'" + "," + "Contect=" + "N'" + CKEditor1.Text + "'" + " WHERE ID='" + Convert.ToInt16(lblID.Text) + "';";
               try
               {
                   _command.ExecuteNonQuery();
               }
               catch (Exception ex)
               {
                   Response.Write(ex.Message);
               }
           }
Posted
Comments
Thanks7872 14-Oct-13 7:09am    
Use debugger and try to find out whats going on inside this code block.
Ameer A. Lawi 14-Oct-13 8:30am    
i did .. the old value passed there :(

1 solution

Two things:
1) 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. Particularly in a web based application, where someone from the other side of the world could destroy your database without even trying hard...
2) Check your Page Load event: I am pretty sure you don't check for IsPostback[^] ebfore setting up your page - which means when the user clicks the button, you overwrite his new values before the Button_Click event is actioned...
 
Share this answer
 
Comments
Ameer A. Lawi 14-Oct-13 8:31am    
you are right.. but for now i need t know what is the problem .. and how can i solve it in this case .
OriginalGriff 14-Oct-13 9:35am    
Did you check your page load?

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