Solved

datareader and null values in table

Posted on 2006-11-17
2
300 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

13 Experts available now in Live!

Get 1:1 Help Now