Solved

datareader and null values in table

Posted on 2006-11-17
2
302 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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