Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Active Server Pages (ASP)
Expertise: Beginner
Aug 26, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Test for NULL Properly in a Recordset


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 Then
Response.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?


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.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date