Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 749
  • Last Modified:

Null checking for get methods of a datareader?

Hello
   Is there a way to error check the null value of fields returned in the results of an SQL query in ASp.NET. Reason this is important is that the Get methods of the data reader expect the fields to have non-null values.

For instance I need to add the null error checking for the following line of code:
gotName= sreader.GetString(sreader.GetOrdinal("Name")).ToString() <----------wont work if this field was null
 txtCustomer.text=gotName

Also, can someone suggest me a good book on ASP.NET programming that deals with issues like Datareader methods etc ,sorta like a bit more than a beginner but not quite an advanced reference I guess.

thanx
0
LuckyLucks
Asked:
LuckyLucks
3 Solutions
 
Mikal613Commented:
gotName= sreader.GetString(sreader.GetOrdinal("Name") & "").ToString()
0
 
Mikal613Commented:
& "" 

Takes away the null

oe inyour query you can also add the condition where Blah is not null
0
 
willcode4coffeeCommented:
I wrote a simple overloaded method called NullSafe():

Public Shared Function NullSafe(ByVal Value)
        Return NullSafe(Value, TypeCode.String)

    End Function

    Public Shared Function NullSafe(ByVal Value As String) As String

        If Value Is System.DBNull.Value Then
            Return New String("")
        Else
            Return Value

        End If
    End Function

This will return a empty string or zero if a value is null.

M@
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
willcode4coffeeCommented:
To use NullSafe():

Value = NullSafe(Value)

or sMyVal = NullSafe(Value)

0
 
Arthur_WoodCommented:
Rocky Lhotka, in his EXCELLENT book "Visual Basic.NET Business Object" builds what he calls a 'SafeDatraReader' class, that extends the standard DataReader, to not only handle null values, but also to handle date intelligently, and several other 'gotchas'

Rockford Lhotka
Expert One-on-One Visual Basic.NET Business Objects
ISBN: 1590591453
Pub. Date: June 2003
Publisher: APress

don't be put off by the Expert reference in the title.  A very good book to have in your library.

AW
0
 
fahimnxbCommented:
Dear LuckyLucks,

here is what u need...

If Not isDBNULL(sreader.GetString(sreader.GetOrdinal("Name"))) Then
           txtCustomer.text=gotName
EndIf

Regards,
Me
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now