I have a CustomValidator that checks if the text entered in text fields matches the specific fields in the database. All this worked fine before, but since then I changed my page a bit and it doesnβt work anymore. I did not think that I changed everything that would affect this, but apparently I did. All my other validators (required validator fields) work correctly, but my CustomValidator is not responding.
Anyway, here is my code:
CustomValidator:
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken."></asp:CustomValidator>
VB codebehind:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate 'Checking for duplicate course numbers 'get values Dim checkPrefix = txtCoursePrefix.Text Dim checkNum = txtCourseNum.Text 'db connectivity Dim myConn As New OleDbConnection myConn.ConnectionString = AccessDataSource2.ConnectionString myConn.Open() 'select records Dim mySelect As New OleDbCommand("SELECT 1 FROM tableCourse WHERE prefix=? AND course_number=?", myConn) mySelect.Parameters.AddWithValue("@checkPrefix", checkPrefix) mySelect.Parameters.AddWithValue("@checkNum", checkNum) 'execute(Command) Dim myValue = mySelect.ExecuteScalar() 'check if record exists If myValue IsNot Nothing Then CustomValidator1.SetFocusOnError = True args.IsValid = False End If End Sub
Everything works up to CustomValidator1.SetFocusOnError = True and args.IsValid = False. I tested the If statement, and it works correctly, it returns true, and everything I do inside it is executed.
source share