如何在if语句中添加确认

时间:2015-10-19 11:52:10

标签: asp.net

在我的gridview中,我正在插入数据。如果项目重复,那么它将显示错误消息"项目重复"。但是现在我需要显示"重复的项目"消息和另一条消息"您需要插入此数据"。如果用户按是,则需要插入该数据。我的当前代码仅显示重复的项目,不能允许该数据进入。这是我的代码

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();
             }
         }
     }
 }

1 个答案:

答案 0 :(得分:1)