0

I have a requirement, where I want to check whether user is not inserting the same Project, Survey No again and again.

It should fire an alert if it enters the same combination twice on button click

Here is my 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>

I tried the below link, but it was not for the combination. It was just for one textbox value, So it was not working in my case.

check duplicate data with javascript

Kindly let me know how to deal with the combination part

Community
  • 1
  • 1
Nad
  • 4,605
  • 11
  • 71
  • 160

1 Answers1

-1

I have made a few changes to the answer linked by you

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

this is the backend 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;
}
Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47