devxlogo

Test for NULL Properly in a Recordset

Test for NULL Properly in a Recordset

Question:

I have a column in SQL Server created like this:

"vchrFirstName varchar (50) NULL"

However, this code does not produce anything, including any errors, when the record is NULL:

If RS("chrFirstName") = NULL ThenResponse.Write("Empty")ElseResponse.Write(RS("vchrFirstName")+"...some extra text")End If

Strangely, the statement after Else is executed and nothing is printed to the Web page, but I know the record is NULL because a CStr does not work on it as expected. Also, everything after RS(“vchrFirstName”) is not responsed to the client as well. How can I test for NULL properly in this case?

Answer:

You cannot say if something = null. Instead, use the IsNull(object) function:

If IsNull(RS("chrFirstname")) Then...

A much better way to code is to not even use the previous function. Let’s say you want to display a space if the record is null and the contents of the record if the record is not null. All you would need to code is:

   Response.write RS("fieldname") & " "

Now, if the field is null, you will get a ” ” which is the HTML space character. If not, you will get the contents of the field, plus an extra space, which is okay.

See also  5 Ways to Improve Customer Experience
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist