C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to handle 'DBNull' while using a DataReader


Posted Date: 03 May 2008    Resource Type: Articles    Category: Databases
Author: andy robartsMember Level: Gold    
Rating: Points: 0



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="No Date Specified"
else
fixBirthDate="" & sItem & ""
End If
End function

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

--------------------------------------------------------------------------------






Responses

Author: Balasubramaniyam    04 May 2008Member Level: Gold   Points : 2
Use Convert.ToString(objDR("YearBorn"))

//Note if the value of the field null, null value converted in to empty string.

You will not get any error


Author: Nithya    05 May 2008Member Level: Bronze   Points : 2
foreach (DataRow dtRow in dsTable.Tables[0].Rows )

Response.Write(dtRow[0]== DBNull.Value?"Null Value" : dtRow[0].ToString() );




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Learn Views in Sql Server 2000
Previous Resource: How to handle Database Nulls in a DataReader
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use