locked
Page values refresh RRS feed

  • Question

  • User1754860325 posted

    I have got a page where on the page load it populates values to textboxes and dropdowns with an option to update info but when I click on the update button to make changes to the info, it does not record the new info.

     

    code Behind

    protected void btnUpdate_Click(object sender, EventArgs e)
        {
            MembershipUser currentUser = Membership.GetUser();
            Guid customerId = (Guid)currentUser.ProviderUserKey;
     
            
            using (SqlConnection conn = new SqlConnection(@"Data Source=GMBOAT-PC\EMS_AFRICA;Integrated Security=True; User ID=***; password=***"))
            {
                Page.DataBind();
                string query = "UPDATE StaffMeasurements SET Head = @hd, Neck = @nk, Arm = @am, Torso = @ts, Waist = @wt,";
                query += " Leg = @lg, BodyWeight = @bw, BodyLength = @bl, Cap = @cp, Shirt = @st,";
                query += " Jersey = @js, Jacket = @jkt, TShirt = @tst, Pants = @pts, Shoe = @se,";
                query += " FlashHood = @fh, Tunic = @tc, BunkerPants = @bp, FireGlove = @fg, SaftyShoe = @ss, Comments = @tts";
                query += " WHERE Userid = @ID";
     
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@ID", customerId);
                cmd.Parameters.AddWithValue("@hd", txtHead.Text);
                cmd.Parameters.AddWithValue("@nk", txtNeck.Text);
                cmd.Parameters.AddWithValue("@am", txtArm.Text);
    Is there anything else I can try?
    Wednesday, July 31, 2013 5:18 PM

Answers

  • User-1300320957 posted

    Can you give the values that you want to see on page load with in

    if(!isPostback){

    }

    Hope Helps

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 4, 2013 4:45 AM

All replies

  • User-760709272 posted

    Does it work without Page.DataBind() ?

    Wednesday, July 31, 2013 5:25 PM
  • User1754860325 posted

    No

    Wednesday, July 31, 2013 5:37 PM
  • User-760709272 posted

    Do the parameters you are adding have the values you expect?  Is the customerId the right id?  Does the update code actually run?  Do you get exceptions or do you just not see the data being updated?

     

    Edit: are you executing the command?  Is that code just not included?  You need to

    cmd.ExecuteNonQuery();

    possibly open the connection too

    conn.Open();

    cmd.ExecuteNonQuery();

    Wednesday, July 31, 2013 5:49 PM
  • User-1091252720 posted

    As you describe above, I thought out that there are some following cases that can cause the problem "no record new info to the database":

    1. The values of parameters added to query can be nullable: In this case, you should pass the value as Db.Null.Value instead of the typical nullvalue. 
      //For instance
      //Get value variable as example
      if(value == null)
      {
          cmd.Parameters.AddWithValue("@hd", DBNull.Value);
      }
      else
      {
          cmd.Parameters.AddWithValue("@hd", value);
      }
    2. Maybe you didn't make the call to run the query to the database: for this, you have to call cmd.ExecuteNonQuery() method to run the query. If this method returns 0, it has been updated unsuccessfully. Otherwise, if it returns 1, it has been updated successfully and recorded new info. 
      var affectedRows = cmd.ExecuteNonQuery();
      if(affectedRows > 1)
      {
          //Updated successfully and recorded a new info.
      }
      else
      {
          //Failed to update
      }
    3. Error from your sql script: you should try... catch in your sql script to catch error. 
      try{
         int result = cmd.ExecuteNonQuery();
         //...
      }
      catch(SqlException e)
      {
         //Handle error...
      }

    Hope this will help you!!Smile

    Thursday, August 1, 2013 6:49 AM
  • User1754860325 posted

    I have this code after the code behind shown above:

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                Response.Redirect("~/.../...");
    }
    When I Debug the update statement I can see the old values 25 instead of 35 my new value from a textbox.
    Thursday, August 1, 2013 8:06 AM
  • User-1091252720 posted

    When you see old values, this indicates that the new value didn't push to the database. I think that you should remove the code line Response.Redirect("~/..../....") for the testing purpose. And then you can find the root cause by trying as the complete code as follows:

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
            MembershipUser currentUser = Membership.GetUser();
            Guid customerId = (Guid)currentUser.ProviderUserKey;
     
            
            using (SqlConnection conn = new SqlConnection(@"Data Source=GMBOAT-PC\EMS_AFRICA;Integrated Security=True; User ID=***; password=***"))
            {
                try {
                    conn.Open();
                    if(conn.State == ConnectionState.Open)//only accept connection was opened
                    {
                        Page.DataBind();
                        string query = "UPDATE StaffMeasurements SET Head = @hd, Neck = @nk, Arm = @am, Torso = @ts, Waist = @wt,";
                        query += " Leg = @lg, BodyWeight = @bw, BodyLength = @bl, Cap = @cp, Shirt = @st,";
                        query += " Jersey = @js, Jacket = @jkt, TShirt = @tst, Pants = @pts, Shoe = @se,";
                        query += " FlashHood = @fh, Tunic = @tc, BunkerPants = @bp, FireGlove = @fg, SaftyShoe = @ss, Comments = @tts";
                        query += " WHERE Userid = @ID";
     
                        SqlCommand cmd = new SqlCommand(query, conn);
                        cmd.Parameters.AddWithValue("@ID", customerId);
                        cmd.Parameters.AddWithValue("@hd", txtHead.Text);
                        cmd.Parameters.AddWithValue("@nk", txtNeck.Text);
                        cmd.Parameters.AddWithValue("@am", txtArm.Text);
                        //...
                        
                        //Send sql script to database engine and run over there
                        var result = cmd.ExecuteNonQuery();
                        if(result > 0)
                        {
                            //Updated successfully
                        }
                        else {
                            //Failed to update. Maybe customerId doesn't exist in StaffMeasurements table.
                        }
                    }
                }
                catch (SqlException sqlException)
                {
                     if(sqlException.InnerException != null)
                     {
                         //Output SQL internal error message to the response to display on the web page.
                     }
                     //Output the SQL typical error message to the response to display on the web page.
                }
                catch (Exception exception)
                {
                     if(exception.InnerException != null)
                     {
                         //Output internal error message to the response to display on the web page.
                     }
                     //Output the typical error message to the response to display on the web page.
                }
                conn.Close();
            }
    }

    Hope this will help you!!Smile

    Thursday, August 1, 2013 11:45 AM
  • User1754860325 posted

    I did all that you did above and put break points in the code at various points. I changed only the txtHead.Text value but it still displayed the old value when I put my mouse over the txtHead.Text.

    Thursday, August 1, 2013 1:39 PM
  • User-1091252720 posted

    You can print the error messages come from these exceptions on your next comment. Let me know and I will help you to find a better idea to resolve it.

    Friday, August 2, 2013 5:28 AM
  • User-1300320957 posted

    Can you give the values that you want to see on page load with in

    if(!isPostback){

    }

    Hope Helps

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 4, 2013 4:45 AM