using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class CustomValidator : System.Web.UI.Page
{
    protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
        Label1.Text = string.Empty;
        ConnectionStringSettings getString = WebConfigurationManager.ConnectionStrings["nwind"] as ConnectionStringSettings;
        if (getString != null)
        {
            string sSql = "select Products.ProductName,Products.QuantityPerUnit, Products.UnitPrice from Products";
            using (SqlConnection conn = new SqlConnection(getString.ConnectionString))
            {
                DataSet ds = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter(sSql, conn);
                adapter.Fill(ds, "Produtos");
                DataView dv = ds.Tables[0].DefaultView;
                string txtProduto;
                args.IsValid = false;

                foreach (DataRowView datarow in dv)
                {
                    txtProduto = datarow["ProductName"].ToString();
                    if (txtProduto.Trim() == args.Value.Trim())
                    {
                        args.IsValid = true;
                        Label1.Text = "Validação OK.";
                    }
                }
            }
        }
    }
}

