Database Nulls with a DataReader

Many times, when you are returning results from a database, there will be nulls. Here’s how to handle it when it happens while using a DataReader:

While objDR.Read
	sAuthor=objDR("Author")
	sBorn=FixBirthDate(objDR("YearBorn"))
End While
DataBind
End Sub

Here’s the FixBirthDate Function:

Function fixBirthDate(sItem) as String
if sItem is System.DBNull.Value Then
	fixBirthDate="<i><font Color='#FF0000'>No Date Specified</font></i>"
else
	fixBirthDate="<b>" & sItem & "</b>"
End If
End function

Then, on the page, you can refer to the items like this:
<%# sAuthor%> – <%# sBorn%>

Here’s a complete Code Sample, showing this in a slightly varied way, but with the same general concept:
Handling Database Nulls with a DataReader

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>