Solved

datareader and null values in table

Posted on 2006-11-17
2
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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