0

I want to create an update function as below, and when I run, no errors occur, all the values also remain unchanged and I wonder why.

This is the code behind :

        protected void Button2_Click(object sender, EventArgs e)
        {
                if (!IsPostBack)
                {
                    string sql = "UPDATE EUTHANASIA SET [DESC] = @DESC, MEDICINE = @MEDICINE, DOSE = @DOSE, UOM = @UOM, STATUS = @STATUS, EXECUTIONDATE = @EXECUTIONDATE, UPDATEDDATE = @UPDATEDDATE WHERE EUTHANASIAID = @EUTHANASIAID";

                    SqlConnection con = new SqlConnection(cs);

                    SqlCommand cmd = new SqlCommand(sql, con);

                    var culture = new CultureInfo("ms-MY");
                    string description = Convert.ToString(ddl_Desc.SelectedItem.Value);
                    string medicine = Convert.ToString(ddl_med.SelectedItem.Value);
                    float dose = (float)Convert.ToDouble(txt_Dose.Text);
                    string uom = Convert.ToString(ddl_Uom.SelectedItem.Value);
                    string status = Convert.ToString(ddl_Status.SelectedItem.Value);
                    DateTime executionDate = Convert.ToDateTime(txt_ExeDate.Text, culture);
                    DateTime updatedDate = DateTime.Now;

                    cmd.Parameters.AddWithValue("@DESC", description);
                    cmd.Parameters.AddWithValue("@MEDICINE", medicine);
                    cmd.Parameters.AddWithValue("@DOSE", dose);
                    cmd.Parameters.AddWithValue("@UOM", uom);
                    cmd.Parameters.AddWithValue("@STATUS", status);
                    cmd.Parameters.AddWithValue("@EXECUTIONDATE", executionDate);
                    cmd.Parameters.AddWithValue("@EUTHANASIAID", petNoTxt.Text);
                    cmd.Parameters.AddWithValue("@UPDATEDDATE", updatedDate);

                    con.Open();

                    int rowsAffected = cmd.ExecuteNonQuery();

                    Console.Write(rowsAffected);

                    con.Close();
                  
                    Response.Redirect("BO_EuthanasiaSummary.aspx");
                
                }
        }

Could you please help me to find out the issue with this? Your efforts are much appreciated. Thank you!

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
  • The following may be helpul: https://stackoverflow.com/a/68385968/10024425 and https://stackoverflow.com/a/70307947/10024425 – Tu deschizi eu inchid Sep 11 '22 at 18:01
  • visit this link might help https://learn.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling – kimiahdri Sep 11 '22 at 18:06
  • If you put a breakpoint inside that `if`, does it get hit at the right time? You are testing for NOT postback, while this must probably be executed ON postback – Hans Kesting Sep 11 '22 at 20:11
  • On second thought, why are you even checking for PostBack in a button handler that *needs* a postback? – Hans Kesting Sep 11 '22 at 20:13

1 Answers1

0

Please check petNoTxt.Text. Maybe it couldn't find its value to assign into @EUTHANASIAID. Or it needs to be cast into the string or int.