I have GridView. When i have one row in GridView then save value into database, it redirect page one time, if i have two rows in GridView, then it redirect two time, which should not be.
Just i want to redirect one time
protected void btn_Save_Click(object sender, EventArgs e)
{
if (txtentdate.Text == "")
{
Response.Write("<script language='javascript'>alert('Please Input Date');</script>");
txtentdate.Focus();
}
else if (ddldebit.SelectedValue == "0")
{
Response.Write("<script language='javascript'>alert('Please Select Bank ');</script>");
}
else if (txtchequino.Text == "")
{
Response.Write("<script language='javascript'>alert('Please Input Cheque No.');</script>");
txtchequino.Focus();
}
else if (txtchequdate.Text == "")
{
Response.Write("<script language='javascript'>alert('Please Input Cheque Date');</script>");
txtchequdate.Focus();
}
else if (txtamount.Text == "")
{
Response.Write("<script language='javascript'>alert('Please Input Amount');</script>");
txtamount.Focus();
}
//decimal totalDebit = Convert.ToDecimal(hfDebit.Value);
//decimal totalCredit = Convert.ToDecimal(txtamount.Text);
else if (gvtrans.Rows.Count == 0)
{
Response.Write("<script language='javascript'>alert('Please Select head');</script>");
}
else if (Convert.ToDecimal(hfDebit.Value) != Convert.ToDecimal(hfCredit.Value))
{
Response.Write("<script language='javascript'>alert('Debit and Credit Amount must be Equal');</script>");
}
else
{
using (SqlCommand cmd1 = new SqlCommand("Sp_Insert_tbl_Transection", con))
{
loadtransID();
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@Trans_ID", lbid.Text);
cmd1.Parameters.AddWithValue("@Trans_Date", txtentdate.Text);
cmd1.Parameters.AddWithValue("@Trans_Type_ID", 1);
cmd1.Parameters.AddWithValue("@Level_Five_ID_D", DBNull.Value);
cmd1.Parameters.AddWithValue("@Level_Five_ID_C", ddldebit.SelectedValue);
cmd1.Parameters.AddWithValue("@Trans_Remarks", txtremarks.Text);
cmd1.Parameters.AddWithValue("@trans_Container_No", string.IsNullOrEmpty(txtcontainer.Text) ? (object)DBNull.Value : txtcontainer.Text);
cmd1.Parameters.AddWithValue("@Trans_Amount", txtamount.Text.Trim());
cmd1.Parameters.AddWithValue("@Cheque_No", txtchequino.Text.Trim());
cmd1.Parameters.AddWithValue("@Cheque_Date", txtchequdate.Text);
cmd1.Parameters.AddWithValue("@Emp_ID", lbempid.Text);
cmd1.Parameters.AddWithValue("@E_Date", DateTime.Now.ToString());
cmd1.Parameters.AddWithValue("@E_Time", DateTime.Now.ToString("hh:mm:ss"));
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
foreach (GridViewRow row in gvtrans.Rows)
{
TextBox transId = row.FindControl("txtcode") as TextBox;
DropDownList ddltype = row.FindControl("ddltype") as DropDownList;
TextBox debit = row.FindControl("txtDebit") as TextBox;
TextBox credit = row.FindControl("txtCredit") as TextBox;
DropDownList ddlfourdr = row.FindControl("ddlfourdr") as DropDownList;
TextBox remarks = row.FindControl("txtremarks") as TextBox;
string message = "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);
string Url = "/Bpprt.aspx?ID=" + lbid.Text;
Response.Write("<script language='javascript'>window.open('" + Url + "','_blank','');");
Response.Write("</script>");
if (transId.Text == "")
{
Response.Write("<script language='javascript'>alert('Please Select Head');</script>");
}
else if (debit.Text != "0")
{
using (SqlCommand cmd1 = new SqlCommand("Sp_Insert_tbl_Transection", con))
{
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@Trans_ID", lbid.Text);
cmd1.Parameters.AddWithValue("@Trans_Date", txtentdate.Text);
cmd1.Parameters.AddWithValue("@Trans_Type_ID", 1);
cmd1.Parameters.AddWithValue("@Level_Five_ID_D", ddlfourdr.SelectedValue);
cmd1.Parameters.AddWithValue("@Level_Five_ID_C", DBNull.Value);
cmd1.Parameters.AddWithValue("@Trans_Remarks", txtremarks.Text);
cmd1.Parameters.AddWithValue("@R_Remarks", remarks.Text);
cmd1.Parameters.AddWithValue("@trans_Container_No", string.IsNullOrEmpty(txtcontainer.Text) ? (object)DBNull.Value : txtcontainer.Text);
cmd1.Parameters.AddWithValue("@Trans_Amount", debit.Text.Trim());
cmd1.Parameters.AddWithValue("@Cheque_No", txtchequino.Text.Trim());
cmd1.Parameters.AddWithValue("@Cheque_Date", txtchequdate.Text);
cmd1.Parameters.AddWithValue("@Emp_ID", lbempid.Text);
cmd1.Parameters.AddWithValue("@E_Date", DateTime.Now.ToString());
cmd1.Parameters.AddWithValue("@E_Time", DateTime.Now.ToString("hh:mm:ss"));
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
}
else if (credit.Text != "0")
{
using (SqlCommand cmd1 = new SqlCommand("Sp_Insert_tbl_Transection", con))
{
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@Trans_ID", lbid.Text);
cmd1.Parameters.AddWithValue("@Trans_Date", txtentdate.Text);
cmd1.Parameters.AddWithValue("@Trans_Type_ID", 1);
cmd1.Parameters.AddWithValue("@Level_Five_ID_C", ddlfourdr.SelectedValue);
cmd1.Parameters.AddWithValue("@Level_Five_ID_D", DBNull.Value);
cmd1.Parameters.AddWithValue("@Trans_Remarks", txtremarks.Text);
cmd1.Parameters.AddWithValue("@R_Remarks", remarks.Text);
cmd1.Parameters.AddWithValue("@trans_Container_No", string.IsNullOrEmpty(txtcontainer.Text) ? (object)DBNull.Value : txtcontainer.Text);
cmd1.Parameters.AddWithValue("@Trans_Amount", credit.Text.Trim());
cmd1.Parameters.AddWithValue("@Cheque_No", txtchequino.Text.Trim());
cmd1.Parameters.AddWithValue("@Cheque_Date", txtchequdate.Text);
cmd1.Parameters.AddWithValue("@Emp_ID", lbempid.Text);
cmd1.Parameters.AddWithValue("@E_Date", DateTime.Now.ToString());
cmd1.Parameters.AddWithValue("@E_Time", DateTime.Now.ToString("hh:mm:ss"));
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
}
}
}
}