ASP.net中的CompareValidator将文本框中的用户输入值与数据库进行比较

时间:2016-01-04 19:20:51

标签: c# asp.net sql-server validation

我正在尝试验证哪里可以检查TextBox中的哪些用户是我的数据库中有效的PartNumber。

我的asp页面看起来像this 。和背后的代码是

<table width="50%" border="1" align="center">
        <tr>
            <td width="30%" class="style1">
                <b>Part Number :</b>
            </td>
            <td class="style1">
                 <asp:TextBox ID="lpartno" runat="server" Width="300px"></asp:TextBox>
          <cc1:AutoCompleteExtender ServiceMethod="GetCompletionListPartno" MinimumPrefixLength="1"
                    CompletionInterval="1" EnableCaching="false" TargetControlID="lpartno"
                    ID="AutoCompleteExtender2" runat="server" FirstRowSelected="false">
                </cc1:AutoCompleteExtender>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="lpartno"  TargetControlID="lpartno"  ErrorMessage="PartNumber is Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td width="30%">
                <b>Reveived Date :</b>
            </td>
            <td>
                <asp:TextBox ID="lReceivedDate" runat="server" CausesValidation="True" ></asp:TextBox>
                <cc1:CalendarExtender ID="lReceivedDate_CalendarExtender" runat="server" Enabled="True"
                    TargetControlID="lReceivedDate">
                </cc1:CalendarExtender>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="lReceivedDate"  TargetControlID="lReceivedDate"  ErrorMessage="Reveived Date is Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td width="30%">
                <b>PO Number :</b>
            </td>
            <td>

                    <asp:TextBox ID="lPONo" runat="server" Width="300px"></asp:TextBox>
          <cc1:AutoCompleteExtender ServiceMethod="GetCompletionListPONo" MinimumPrefixLength="1"
                    CompletionInterval="1" EnableCaching="false" TargetControlID="lPONo"
                    ID="AutoCompleteExtender3" runat="server" FirstRowSelected="false">
                </cc1:AutoCompleteExtender>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="lPONo"  TargetControlID="lPONo"  ErrorMessage="PO Number is Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>

我想验证我们可以检查用户输入的部件号是否在我的数据库表中。如果没有,它应该是一些错误消息。

请告知我将如何实现这一目标! 提前致谢 !!  enter image description here

1 个答案:

答案 0 :(得分:0)

这就是我解决这个问题的方法

 protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
         {
             args.IsValid = false;
             string StaffID = lpartno.Text;
             SqlConnection conn = new SqlConnection();

             conn.ConnectionString = ConfigurationManager.ConnectionStrings["eFoxNetConnectionString"].ConnectionString;

             SqlDataAdapter da = new SqlDataAdapter("select distinct PartNo from dbo.FTX_SAPPO sp with (nolock) where PartNo=@ID",conn);
             da.SelectCommand.Parameters.Add("@ID", SqlDbType.VarChar, 120);
             da.SelectCommand.Parameters["@ID"].Value = StaffID;
             DataSet ds = new DataSet();
             da.Fill(ds);
             if (ds.Tables[0].Rows.Count > 0)
                 args.IsValid = true;
  }