Partager via


How to: Validate Against Values in a Database for ASP.NET Server Controls

You can validate a user's entry against a database to be sure that what the user has entered is a recognized value. To do this, you must write code in a CustomValidator control that looks for data matches in a database.

To validate against a database

  1. Add a CustomValidator control to the page and set the following properties:

    Property

    Description

    ControlToValidate

    The ID of the control you are validating.

    ErrorMessage, Text, Display

    Properties that specify the text and location of the error or errors that will display if the validation fails. For details, see How to: Control Validation Error Message Display for ASP.NET Server Controls.

  2. Create an event handler for the CustomValidator control's ServerValidate event. In the event handler, add code to look through the database and check the user's input against the dataset.

    Note

    If the user leaves a control blank, the control passes the comparison validation. To force the user to enter a value, add a RequiredFieldValidator control as well. For details, see How to: Validate Required Entries for ASP.NET Server Controls.

  3. Add a test in your ASP.NET Web page code to check for validity. For details, see How to: Test Validity Programmatically for ASP.NET Server Controls.

    The following code example shows how you can validate a user's entry by looking it up in a database table. In this instance, the user has entered an e-mail address that is validated against e-mail addresses stored in a table. The custom validation logic loops through the rows in a table that is part of the dataset available to the page.

    Private Sub CustomValidator1_ServerValidate(ByVal _
       source As System.Object, ByVal args As _
       System.Web.UI.WebControls.ServerValidateEventArgs) _
       Handles CustomValidator1.ServerValidate
    
        Dim dv As DataView
        Dim dataset11 As New Data.DataSet
    
        dv = dataset11.Tables(0).DefaultView
    
        Dim datarow As DataRowView
        Dim txtEmail As String
        args.IsValid = False    ' Assume False
        ' Loop through table and compare each record against user's entry
        For Each datarow In dv
            ' Extract e-mail address from the current row
            txtEmail = datarow.Item("Alias").ToString()
            ' Compare e-mail address against user's entry
            If txtEmail = args.Value Then
                args.IsValid = True
                Exit For
            End If
        Next
    End Sub
    
    private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
        DataView dv;
        DataSet dataSet11 = new DataSet();
    
        dv = dataSet11.Tables[0].DefaultView;
        string txtEmail;
        args.IsValid = false;    // Assume False
        // Loop through table and compare each record against user's entry
        foreach (DataRowView datarow in dv)
        {
            // Extract e-mail address from the current row
            txtEmail = datarow["Alias "].ToString();
            // Compare e-mail address against user's entry
            if (txtEmail == args.Value)
            {
                args.IsValid = true;
            }
        }
    }
    

See Also

Concepts

Types of Validation for ASP.NET Server Controls

Other Resources

ASP.NET Validation Controls