在按钮单击事件上检查数据库中的重复值

时间:2016-09-01 06:55:15

标签: javascript c# sql asp.net ajax

我有一个要求,我想检查用户是否一次又一次地插入相同的ProjectSurvey No

如果按钮点击

两次进入相同的组合,则应发出警告

这是我的HTML: -

<td class="label">
                    Project :
                </td>
                <td class="field" style="width: 10%;">
                    <asp:DropDownList ID="ddlProject" runat="server" Width="80%" AutoPostBack="true"
                        OnSelectedIndexChanged="ddlProject_SelectedIndexChanged">
                        <asp:ListItem Value="0">--Select--</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td class="label">
                    Survey No :
                </td>
                <td class="field">
                    <asp:TextBox ID="txtSurvey1" runat="server" Width="80%" ReadOnly="true"></asp:TextBox>
                </td>

我尝试了下面的链接,但它不适用于组合。它只是一个文本框值,所以它不适用于我的情况。

check duplicate data with javascript

请让我知道如何处理组合部分

1 个答案:

答案 0 :(得分:-1)

我对您链接的答案做了一些更改

    var projval=$('#ddlProject').val();
    var survey=$('#txtSurvey1').val();
    $.ajax({
     type: "POST",
     url: "YourPage.aspx/DoesDataExist",
    data: JSON.stringify({
        proj :projval, // the names of the properties must match with the parameters in the backend function
        surv :survey
       }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
     success: function(msg) {
    if(msg.d) {
        // This is a duplicate, alert user with message
        // Block the server-side click from happening with return false;
        return false;
      }
   }
  });

这是后端webservice

  [WebMethod]
  public static bool DoesDataExist(string proj, string surv )
  {
      SqlCommand commandrepeat1 = new SqlCommand("Select * from table where project="+proj+" and Survey="+surv+" ");
      commandrepeat1.Connection = objconnection;
      objconnection.Close();
      objconnection.Open();
      SqlDataReader drmax1;
      drmax1 = commandrepeat1.ExecuteReader();
      drmax1.Read();
      if (drmax1.HasRows)
      {
          objconnection.Close();
          return true;
      }

      objconnection.Close();
      return false;
}