User account already exists using Customvalidator

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I've suprisingly not been able to find examples on this. I'm creating
a user account setup page, and the validators work fine on all the
other fields. But now I'm creating a customvalidator (for the first
time) and it is not doing anything. The page needs to see if a
username already exists. Am I doing this right?


<asp:textbox TabIndex="1" ID="tLogin" MaxLength="40" runat="server" />
<asp:customvalidator id="Customvalidator1" runat="server"
EnableClientScript="False" Font-Bold="True" ErrorMessage="Username
already exists." ControlToValidate="tLogin"
OnServerValidate="CheckUser" Display="Dynamic"/>



void CheckUser(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
TextBox b;
b = fLogin.FindControl("tLogin") as TextBox;
String lg = b.Text;
String strConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=" + Server.MapPath("info.mdb") + ";";
OleDbConnection Conn = new OleDbConnection(strConn);
Conn.Open();
String strSQL = "SELECT * FROM Customers WHERE Login = '" + lg
+ "'";
OleDbCommand Cmd = new OleDbCommand(strSQL, Conn);
OleDbDataReader Dr =
Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (Dr.HasRows)
{
args.IsValid = false;
} else args.IsValid = true;
Conn.Close();
}
 
Actually even when I set the custom event handler to just return
false, it still does not kick in with the error message. Why isn't it
doing anything? Any help would be appreciated.
 
Nevermind, I gave up on the server side customvalidator and placed the
query code in the submit button handler.
 
Custom Validator ServerValidate event will not fire if text is empty. By
default ValidateEmptyText property is set to False. Set it to true and try
again.
 
Back
Top