Hello Everyone. My code is given below. I want to show the Successfully added message after checking all the validation control. But it showing the message before checking all validation control.
protected void Button2_Click(object sender, EventArgs e)
{
Label Total = (Label)GridView1.FooterRow.FindControl("Label9");
int total = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (chk.Checked == true)
{
TextBox TextBox1 = (TextBox)GridView1.Rows[i].FindControl("TextBox3");
if (TextBox1.Text != "")
{
total += Convert.ToInt32(TextBox1.Text);
}
}
Total.Text = total.ToString();
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Label lab = (Label)GridView1.Rows[i].Cells[0].FindControl("Label1");
Label ColTotal = (Label)GridView1.FooterRow.FindControl("Label9");
TextBox quantity = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TextBox2");
TextBox TAmount = (TextBox)GridView1.Rows[i].Cells[2].FindControl("TextBox3");
string amount = quantity.Text;
DropDownList ddl = (DropDownList)GridView1.Rows[i].Cells[3].FindControl("DropDownList1");
TextBox ph = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TextBox6");
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("Checkbox1");
CustomValidator com = (CustomValidator)GridView1.Rows[i].Cells[1].FindControl("CustomValidator1");
if (chk.Checked == true)
{
using (SqlCommand comm = new SqlCommand("select [Serial_Number_Required] from Products where Product=@Product ", con))
{
con.Open();
SqlParameter para = null;
para = new SqlParameter();
para.ParameterName = "@Product";
para.Value = lab.Text;
comm.Parameters.Add(para);
SqlDataReader reade = comm.ExecuteReader();
while (reade.Read())
{
Session["Serial_Number_Required"] = Convert.ToString(reade["Serial_Number_Required"]);
}
con.Close();
string serials = ph.Text;
string[] sl = serials.Split(',');
if (Session["Serial_Number_Required"].ToString() == "Y")
{
if (quantity.Text.Trim() != "" && TAmount.Text != "")
{
for (int j = 0; j < sl.Length; j++)
{
for (int k = i; k < sl.Length - 1; k++)
{
string str = "select count(*)from Inventory where [Serial Number]='" + sl[j].ToString() + "'";
con.Open();
SqlCommand commm = new SqlCommand(str, con);
int count = Convert.ToInt32(commm.ExecuteScalar());
con.Close();
if (count == 0 && sl[j].ToString() != sl[k + 1].ToString())
{
con.Open();
string query1 = "insert into Inventory ([Invoice Number],Product,[Serial Number],Amount,Status) values('" + TextBox5.Text + "', '" + lab.Text + "','" + sl[j].ToString() + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "')";
SqlCommand cmd = new SqlCommand(query1, con);
cmd.ExecuteNonQuery();
con.Close();
con.Open();
string query = "insert into FSPL_Inventory ([Invoice Number],[Date],[Total Amount],Product,[Serial Number],Amount,Status) values('" + TextBox5.Text + "','" + Label7.Text + "','" + ColTotal.Text + "','" + lab.Text + "','" + sl[j].ToString() + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "')";
SqlCommand comd = new SqlCommand(query, con);
comd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
if (Session["Serial_Number_Required"].ToString() == "N")
{
if (quantity.Text.Trim() != "" && TAmount.Text != "")
{
for (int j = 0; j < Convert.ToInt16(quantity.Text); j++)
{
string S = "";
con.Open();
string query1 = "insert into Inventory ([Invoice Number],Product,Amount,Status,[Serial Number]) values('" + TextBox5.Text + "', '" + lab.Text + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "','" + S.ToString() + "')";
SqlCommand cmd = new SqlCommand(query1, con);
cmd.ExecuteNonQuery();
con.Close();
con.Open();
string query = "insert into FSPL_Inventory ([Invoice Number],[Date],[Total Amount],Product,Amount,Status,[Serial Number] ) values('" + TextBox5.Text + "','" + Label7.Text + "','" + ColTotal.Text + "','" + lab.Text + "','" + Convert.ToInt32(TAmount.Text) / Convert.ToInt32(quantity.Text) + "','" + ddl.SelectedValue + "','" + S.ToString() + "')";
SqlCommand comd = new SqlCommand(query, con);
comd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Successfully added');</script>");
}
}
}
}
}
bind();
}
}