Insert and update on single ASP.Net Button Click is not working.
protected void Button2_Click6(object sender, EventArgs e)
{
string emp_id = Label1.Text.Trim();
string agncy_nm = TextBox12.Text.Trim();
string nit_no = TextBox16.Text.Trim();
string po_no = TextBox17.Text.Trim();
string bg_typ = TextBox18.Text.Trim();
string actn_pnding = "na";
string schdle_dt = "na";
string release_of_emd_bg = "na";
string emd_bg_rlesed = "na";
string bg_no = TextBox19.Text.Trim();
string bg_issue_dt = TextBox20.Text.Trim();
string bg_exp_dt = TextBox21.Text.Trim();
string bg_issu_bnk = TextBox22.Text.Trim();
string bg_rcv_midia = TextBox23.Text.Trim();
string frst_rcv_whom = TextBox24.Text.Trim();
string bg_hnd_ovr = TextBox25.Text.Trim();
string is_biddr_sucsful = DropDownList1.Text.Trim();
string is_biddr_to_sbmtd_sd_pbg = DropDownList3.Text.Trim();
string bidder_sbmt_sd_pbg = DropDownList5.Text.Trim();
string rmk = Label16.Text.Trim();
string stts = Label9.Text.Trim();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "spInsert_bgms2";
using (SqlConnection con = new SqlConnection(conString))
{
DateTime issueDate = Convert.ToDateTime(Request.Form[TextBox6.UniqueID]);
DateTime expiryDate = Convert.ToDateTime(Request.Form[TextBox7.UniqueID]);
if (expiryDate < issueDate)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('BG Expiry Date must be greater then BG Issue Date')", true);
}
else
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@emp_id", emp_id);
cmd.Parameters.AddWithValue("@agncy_nm", agncy_nm);
cmd.Parameters.AddWithValue("@bg_typ", bg_typ);
cmd.Parameters.AddWithValue("@nit_no", nit_no);
cmd.Parameters.AddWithValue("@po_no", po_no);
cmd.Parameters.AddWithValue("@bg_no", bg_no);
cmd.Parameters.AddWithValue("@bg_issue_dt", bg_issue_dt);
cmd.Parameters.AddWithValue("@bg_exp_dt", bg_exp_dt);
cmd.Parameters.AddWithValue("@bg_issu_bnk", bg_issu_bnk);
cmd.Parameters.AddWithValue("@bg_rcv_midia", bg_rcv_midia);
cmd.Parameters.AddWithValue("@frst_rcv_whom", frst_rcv_whom);
cmd.Parameters.AddWithValue("@bg_hnd_ovr", bg_hnd_ovr);
cmd.Parameters.AddWithValue("@actn_pnding", actn_pnding);
cmd.Parameters.AddWithValue("@schdle_dt", schdle_dt);
cmd.Parameters.AddWithValue("@is_biddr_sucsful", is_biddr_sucsful);
cmd.Parameters.AddWithValue("@is_biddr_to_sbmtd_sd_pbg", is_biddr_to_sbmtd_sd_pbg);
cmd.Parameters.AddWithValue("@bidder_sbmt_sd_pbg", bidder_sbmt_sd_pbg);
cmd.Parameters.AddWithValue("@release_of_emd_bg", release_of_emd_bg);
cmd.Parameters.AddWithValue("@emd_bg_rlesed", emd_bg_rlesed);
cmd.Parameters.AddWithValue("@rmk", rmk);
cmd.Parameters.AddWithValue("@stts", stts);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
string Message = "BG Details has been saved successfully.";
string script = "window.onload = function(){ alert('";
script += Message;
script += "');";
script += "window.location = '";
script += Request.Url.AbsoluteUri;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
Response.Redirect("main.aspx", true);
con.Close();
con.Dispose();
}
string bg_id = Label5.Text.Trim();
string stts1 = Label9.Text.Trim();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(constr))
using (SqlCommand cmd = new SqlCommand("UPDATE bgms SET stts=@stts WHERE bg_id = @bg_id"))
{
//SqlCommand cmd = new SqlCommand(SqlCommand);
cmd.Parameters.AddWithValue("@bg_id", bg_id);
cmd.Parameters.AddWithValue("@stts", stts);
cmd.Connection = con1;
con1.Open();
cmd.ExecuteNonQuery();
con1.Close();
}
}
}