Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4272548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:35:32+00:00 2026-05-21T07:35:32+00:00

I have a form where two fields on the first page of the form

  • 0

I have a form where two fields on the first page of the form make up the primary key. I want to check for duplicate values before attempting to insert the record, since I don’t want the user to go all the way through the form only to find out they can’t submit it. So I’m trying to check for duplicate values when the user tries to go to the next page of the form. I wasn’t quite sure how to do it, and sure enough I’m getting an error. (“Object reference not set to an instance of an object.”) The problem is apparently in my if statement, “If myValue.Length > 0 Then”, but I’m not sure what needs to be in place of that.

Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate

    'get values
    Dim checkPrefix = txtCoursePrefix.Text
    Dim checkNum = txtCourseNum.Text

    'db connectivity
    Dim myConn As New OleDbConnection
    myConn.ConnectionString = AccessDataSource1.ConnectionString
    myConn.Open()

    'select records
    Dim mySelect As New OleDbCommand("SELECT prefix, course_number FROM tableCourse WHERE prefix='checkPrefix' AND course_number='checkNum'", myConn)

    'execute(Command)
    Dim myValue As String = mySelect.ExecuteScalar()
    'check if record exists
    If myValue.Length > 0 Then
        CustomValidator1.ErrorMessage = "some exp text"
        CustomValidator1.SetFocusOnError = "true"
        CustomValidator1.IsValid = "false"
    End If

End Sub

Thought I’d post the final solution:

'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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:35:33+00:00Added an answer on May 21, 2026 at 7:35 am

    This error indicates that the content of myValue variable is null. If it’s null you can’t use Length property (or any other property for that matter) on it. You have to check for null explicitly:

    If myValue IsNot Nothing Then
    

    EDIT 1
    Your sql query is wrong. I don’t know what would be the right query, as I don’t know your database, but I think you intender to write this:

    Dim mySelect As New OleDbCommand("SELECT prefix, course_number FROM tableCourse WHERE prefix=" + checfkPreix + " AND course_number=" + checkNum, myConn)
    

    or something to that effect. You might want to consider using string.Format function for forming the string. And you also need to make sure that there is some kind of protection against SQL Injection, since you form your query from user input. In your case using of OleDbParameter might be appropriate.

    Edit 2

    You also right to mention that there might be a problem with ExecuteScalar. ExecuteScalar is supposed to return a single value and your select query are returning two (prefix and course_number). Change it so that it returns a single parameter SELECT prefix FROM or simply SELECT 1 FROM and then the rest of the query:

    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)
    

    Edit 3
    You are not setting failed validation properly in your validator.
    Add

     args.IsValid = False 
    

    inside your if statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two form fields on my view page, a date input (with a
I have an html form with two input fields, in first one user puts
I have a JSF page with two forms (pseudo-code below). First form is for
I have a form with two input text fields: <input id=ModelName_test_0 name=ModelName[test][0] type=text> <input
I have a form with (at least) the following two fields: country club The
Imagine that I have a form in a flash application with two fields, input1
I have two fields on the form ( forgotpassword form ) username and email
Say I have two fields in a new or edit form: <%= f.text_field :email
I have a usual login form consisting of two input fields, one for login,
I have two select fields in a form. Every time one of these select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.