I'm sure you're all familiar with the various ASP.NET validation controls. These allow you to validate user input for required entries (RequiredFieldValidator), against known values or data types (CompareValidator), against patterns (RegularExpressionValidator) and so on. But what if you need to validate using values stored in a database, where validation succeeds or fails based on the existence or (non-existence) of the entered value?
Here, we'll create a very simple page with only a textbox, a button, a label, a couple of validation controls, and a data source. (I'm using a SqlDataSource for simplicity; obviously, you can use any data source you have, such as ObjectDataSource, DataSets, or whatever.) This will be part of a softball league signup site. The user must enter a unique name for his or her team. If that name is already used by another team, the user is warned and asked to pick another name. If the name is unique, the application accepts it.
For this example, we're going to be using a real simple database -- only one table, containing the names of the existing teams. (Please excuse the extreme lameness of the team names I came up with.)
Here is the ASPX code for the page shown above:
You'll notice there are two validation controls used to validate the textbox. One of them is a RequiredFieldValidator. We need this because we obviously can't test against non-existent input. The other is a CustomValidator. The CustomValidator lets us define our own server side validation logic, which is wired with the OnServerValidate event handler. In the server side validation function, we're going to search the database for the entered value. If it's there, validation will fail and the CustomValidator's ErrorMessage will be displayed. If it's a unique name, validation passes and the user is notified to that effect.
The actual logic is pretty simple. The ServerValidateEventArgs object has a boolean property IsValid which gets or sets whether the validation passes. We set args.IsValid = true at the beginning. We then loop through the collection of data rows returned by the SqlDataSource. For each row, we check the value of the TeamName column. If the value from the row matches the entered value we are validating (specified by args.Value), then args.IsValid is set to false and the loop is terminated. In this case, validation fails. If no matches are found, the validation passes.