2
Answers

how to add confirmation in if statement

In my gridview I am inserting datas. If items repeated then it will show error message that "item repeated".But now I need to show "item repeated" message and another message that "you need to insert this data". If user press yes then that data need to be inserted.My current code only displays item repeated and cannot permit that data to enter. Here is my code

protected void AddNewCustomer(object sender, EventArgs e)
    {
        Control control = null;
        if (GridView1.FooterRow != null)
        {
            control = GridView1.FooterRow;
        }
        else
        {
            control = GridView1.Controls[0].Controls[0];
        }
        string SlNo = (control.FindControl("txtSlNo") as TextBox).Text;
        string Code = (control.FindControl("txtcode") as TextBox).Text;
        string details = (control.FindControl("txtdetails") as TextBox).Text;
        string Qty = (control.FindControl("txtqty") as TextBox).Text;
        using (SqlConnection con = new SqlConnection("Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxxx"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                DataTable dt = new DataTable();
                SqlDataAdapter da1;
                da1 = new SqlDataAdapter("select code from Qtattemp where code='" + Code + "' ", con);
                da1.Fill(dt);            
                if (dt.Rows.Count > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                        "alertMessage",
                        "alert('Item Repeated');", true);
                }
                else
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "INSERT INTO [Qtattemp] VALUES(@Code,@details,@Qty,@SlNo)";
                    cmd.Parameters.AddWithValue("@SlNo", SlNo);
                    cmd.Parameters.AddWithValue("@Code", Code);
                    cmd.Parameters.AddWithValue("@details", details);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    GridView1.DataBind();
                    BindData();
                    con.Close();
                }
            }
        }
    }

Answers (2)