Solved

datareader and null values in table

Posted on 2006-11-17
2
301 Views
Last Modified: 2010-04-23
Hey guys

Im confused once again

I have a winform that is populated by a datareader...it works ok until the table it reads has a field with a null value

while reader.read
           lblchemicalvalue.text = reader("t_checmialprice") ''.........String
           lblChemicalWeight.text = reader("t_chemicalWeight) "..........Integer

end while
everything tools along until i hit a null value in the table... ( its a very old database...no idea of how nulls got there )

do how do i handle the exceptions
Steve
 
0
Comment
Question by:stephenz43
2 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 17969546
Try something like this:

lblchemicalvalue.text = NCheck(reader("t_checmialprice"),"").

Private Function NCheck(ByVal x As Object, ByVal yDefault As Object) As Object
    If x Is System.DBNull.Value Then
      NCheck = yDefault
    Else
      NCheck = x
    End If
  End Function

Or

lblchemicalvalue.text = IIF((reader("t_checmialprice") Is DBNull.Value),"",reader("t_checmialprice")).
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17969737
lblchemicalvalue.text = GetString(reader("t_checmialprice")) ''.........String
lblChemicalWeight.text = GetInteger(reader("t_chemicalWeight)) "..........Integer


Private Function GetString(ByVal fld As Object) As String
    If fld Is System.DBNull.Value  Then
      return ""
    Else
      return System.Convert.Tostring(fld)
    End If
End Function

Private Function GetInteger(ByVal fld As Object) As Integer
    If fld Is System.DBNull.Value  Then
      return 0
    Else
      return System.Convert.ToInteger(fld)
    End If
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now