Null checking for get methods of a datareader?

Posted on 2004-11-17
Last Modified: 2008-02-01
   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

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.

Question by:LuckyLucks
    LVL 48

    Expert Comment

    gotName= sreader.GetString(sreader.GetOrdinal("Name") & "").ToString()
    LVL 48

    Expert Comment

    & ""  

    Takes away the null

    oe inyour query you can also add the condition where Blah is not null
    LVL 4

    Expert Comment

    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("")
                Return Value

            End If
        End Function

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

    LVL 4

    Accepted Solution

    To use NullSafe():

    Value = NullSafe(Value)

    or sMyVal = NullSafe(Value)

    LVL 44

    Assisted Solution

    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.

    LVL 5

    Assisted Solution

    Dear LuckyLucks,

    here is what u need...

    If Not isDBNULL(sreader.GetString(sreader.GetOrdinal("Name"))) Then


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 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

    17 Experts available now in Live!

    Get 1:1 Help Now