doxBumssors
New Member
First of all, I went through this thread already and tried the solution suggested, and may have just not understood something because it did not work for me: ASP.NET Custom Validator Client side & Server Side validation not firingI am trying to validate that a user name entered is in the database. Here is my .aspx code:\[code\]<p> <asp:CustomValidator ID = "ValidateToUser" runat="server" ControlToValidate="ToField" ErrorMessage="This user doesn't exist, try again!" ForeColor = "red" OnServerValidate="ValidateToUser_ServerValidate"> </asp:CustomValidator></p> \[/code\]and here is my code behind: \[code\] protected void ValidateToUser_ServerValidate(object source, ServerValidateEventArgs args) { String userNameSentTo = ToField.Text.ToString(); if (userNameSentTo == string.Empty) { args.IsValid = false; } else { SqlConnection connection = new SqlConnection("Data Source=XXX.XX.XXX; Initial Catalog=XXXXX; User ID=XXXX; Password=XXXXX;"); SqlCommand command = new SqlCommand("SELECT UserName FROM UserDB WHERE UserName = @param1", connection); command.Parameters.Add(new SqlParameter("param1", userNameSentTo)); command.Connection = connection; connection.Open(); string user = (string)command.ExecuteScalar(); if (user != null) { args.IsValid = true; } connection.Close(); connection.Dispose(); }\[/code\]I know this won't do exactly what I want it to but I figured at the very least, it would fire my validation if my text is empty. I've even tried this for the code behind and it still didn't work:\[code\] protected void ValidateToUser_ServerValidate(object source, ServerValidateEventArgs args) { args.IsValid = false; }\[/code\]Completely stumped, any ideas? Did I just miss something from the last thread? Thanks