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();
}
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();
}