Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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.
C#
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();
                  //string Role = (string)(Session["Role"]);
                  // 2. define parameters used in command object
                  SqlParameter para = null;
                  para = new SqlParameter();
                  para.ParameterName = "@Product";
                  para.Value = lab.Text;
                  comm.Parameters.Add(para);

                  //Pass @Pages parameter

                  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++)
                              {
                                  //if (sl[i].ToString() == sl[k + 1].ToString())
                                  //{
                                  //    Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Enter Unique serial Number for '" + lab.Text + "'');", true);
                                  //}
                                  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();
                                  }
                              }
                          }
                        //  Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Items are added successfully into FSPL Inventory');", true);

                      }
                  }
                  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>");
                          }
                         // Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Items are added successfully into FSPL Inventory');", true);
                      }
                  }
              }
          }
          bind();
      }
  }
Posted
Updated 29-Sep-14 22:57pm
v2
Comments
Samatha Reddy G 30-Sep-14 9:16am    
as per your question.... what i understood means validation is not working properly please check once your client side validations. if you have any error then it should be validate client side it will return false, then you wont go to the server side, you will not get that message.

With Response.Write you add a script tag (with your alert in it) just before anything else in your page, so it will be running before anything else (include actual rendering on the client)...
You have to render the script at the end - try ScriptManager[^]...
 
Share this answer
 
Try This .. Defiantly you will Solve you Problem..

C#
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "whatever", "alert('Items are added successfully into FSPL Inventory')", true);




Thanks

AARIF SHAIKH
 
Share this answer
 
By using Script Manager you can show message like alert, confirm, etc
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900