Strongly-Typed DataSet with Is<ColumName>Null and Iif function

Posted on 2006-04-17
Last Modified: 2007-12-19
Here's the story... I'm using VB.Net 2005 with a strongly-typed dataset.  Both Option Explicit and Option Strict are enabled.  The underlying database is SQL 2005 Express (but that's probably not relevant).

In one of my Tables, a string column allows for nulls, so it automatically generated a Is<ColumnName>Null function for me.   So far, so good...

However, when I use the Is<ColumnName>Null inside an IIF() Statement, both the true and false parts get evaluated (even if the expression evaluates to True)

Take a look at the following to see what I'm talking about...

    Overrides Function ToString() As String
        Dim sb As New System.Text.StringBuilder
        Dim dr As SOSOSDataSet.AccountsRow

        For Each dr In _ds.Accounts                             '<- ds = Strongly-Typed DataSet with Account table
            sb.Append("Account=" & dr.Account & vbCr)

            ' Neither of the 2 following lines work!  The dr.Account_FullName method gets evaluated regardless
            ' of the outcome of the expression.  And, when it evaluated to null, it generates an exception
            'sb.Append(vbTab & "FullName=" & CStr(IIf(dr.IsAccount_FullNameNull, "", dr.Account_FullName)) & vbCr)
            'sb.Append(vbTab & "FullName=" & IIf(dr.IsAccount_FullNameNull, "", dr.Account_FullName).ToString & vbCr)

            ' Oddly enough, using an normal If/Else/End if statement works!
            If dr.IsAccount_FullNameNull Then                '<- Automatically generated IsNull function
                sb.Append(vbTab & "FullName=" & vbCr)
                sb.Append(vbTab & "FullName=" & dr.Account_FullName & vbCr)
            End If

            ' Using the old-school IsDBNull function and the "native" DataRow interface
            ' Doesn't work any better inside an IIF statement
            'sb.Append(vbTab & "FullName=" & CStr(IIf(IsDBNull(dr("Account_FullName")), "", dr.Account_FullName)) & vbCr)

            ' However, this works just fine!
            If IsDBNull(dr("Account_FullName")) Then
                sb.Append(vbTab & "FullName=" & vbCr)
                sb.Append(vbTab & "FullName=" & dr.Account_FullName & vbCr)
            End If

            'sb.Append(vbTab & "Description=" & CStr(IIf(dr.IsAccount_FullNameNull, "", dr.Account_FullName))) & vbCr)
            sb.Append(vbTab & "Disabled=" & dr.Account_Disabled.ToString & vbCr)
            sb.Append(vbTab & "LockedOut=" & dr.Account_LockedOut.ToString & vbCr)
            sb.Append(vbTab & "PwdRequired=" & dr.Account_PwdRequired.ToString & vbCr)
            sb.Append(vbTab & "LockedOut=" & dr.Account_PwdChangeable.ToString & vbCr)
            sb.Append(vbTab & "PwdRequired=" & dr.Account_PwdExpires.ToString & vbCr)

        Return sb.ToString
    End Function

I think I'm going crazy... either that or I've found a bug.

Isn't the IIF function supposed to "Short Cut" the evaluation of the arguments?
Question by:graye
    LVL 41

    Author Comment

    Hummm... The MSIL shows that it's evaluating both the true and false parameters, poping them on the stack, and then executing the IIF

          L_0047: ldstr "\tFullName="
          L_004c: ldloc.0
          L_004d: callvirt instance bool SOSOS.SOSOSDataSet/AccountsRow::IsAccount_FullNameNull()
          L_0052: ldstr ""
          L_0057: ldloc.0
          L_0058: callvirt instance string SOSOS.SOSOSDataSet/AccountsRow::get_Account_FullName()
          L_005d: call object [Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::IIf(bool, object, object)

    So, I guess it's just me...

    Is there an "in-line" equivalent of IIF that mirrors the behavior of the "? :" operators in C#
    LVL 10

    Accepted Solution

    When doing the iif, first it evaluates all arguments, then if performs the condition. So, when the expression is null, it will first try to evaluate it regardless of the condition - and that's why it throws an exception. The exception is not being thrown by iif but by your dataset, since iif is trying to retrieve the null value - keep that in mind.

    I would do something like this
    Dim FullName As String = ""
    If Not dr.IsAccount_FullNameNull Then FullName = dr.Account_FullName

    in your string builder:

    sb.append("FullName=" & FullName)



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    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…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    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…

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now