Home > Software design >  Update function no error but also not working in ASP.NET C#
Update function no error but also not working in ASP.NET C#

Time:09-12

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!

CodePudding user response:

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.

  • Related