2015-10-19 103 views
0

在我的gridview插入數據。如果重複的項目,它會顯示錯誤消息,「項目重複」。但現在我需要顯示「項目重複」的消息和另一個消息,「你需要插入這些數據」。如果用戶按是,那麼該數據需要插入。我的當前代碼只顯示重複的項目,並且不允許該數據輸入。這裏是我的代碼如何在if語句中添加確認

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

回答