title: .Net SqlDataReader Item is Null [VB]
body: <p>Ok, this is strange. I ran into this issue tonight when I was adding a feature for our corporate site. </p>
  
  <p>I'm building a custom calendar control that queries our database to display corporate events. Here is the situation, there is an <code>EndDate</code> value stored and on the dev system one of the events have a <code>NULL</code> value. No big deal since it's only a test system, but might as well check before trying to use it anyway on the safe side. I figured the following code would work:</p>
  
  <pre><code>While dr.Read()
    corporateTable.Rows.Add(New Object() { _
      Convert.ToDateTime(dr("EventBeginDate")) _
      , IIf(dr("EventEndDate") Is DBNull.Value, Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), Convert.ToDateTime(dr("EventEndDate"))) _
      , Convert.ToString(dr("EventType")) _
      , Convert.ToString(dr("EventDescription")) _
      , Convert.ToString(dr("EventMessage")) _
    })
  End While
  </code></pre>
  
  <p>But it didn't, I still kept getting the <code>Object cannot be cast from DBNULL</code> error. So I thought it over and came up with this code which works successfully, although I don't like it and think it's ugly.</p>
  
  <pre><code>While dr.Read()
    Dim column As Integer = 0
    While column &lt; dr.FieldCount - 1
      If dr.GetName(column) = "EventEndDate" Then
        Exit While
      End If
  
      column += 1
    End While
    corporateTable.Rows.Add(New Object() { _
      Convert.ToDateTime(dr("EventBeginDate")) _
      , IIf(dr.IsDBNull(column), Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), dr.Item(column)) _
      , Convert.ToString(dr("EventType")) _
      , Convert.ToString(dr("EventDescription")) _
      , Convert.ToString(dr("EventMessage")) _
    })
  End While
  </code></pre>
  
  <p>The thing that really gets me is, at one point I had this:</p>
  
  <pre><code>, IIf(dr.IsDBNull(column), Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), Convert.ToDateTime(dr("EventEndDate"))) _
  </code></pre>
  
  <p>Thinking that it should work because it should only evaluate the <code>dr()</code> if it's not <code>NULL</code>. However, it kept erroring out at the end because the value was in fact <code>NULL</code>.</p>
  
  <p>So to finally get to my question, sorry for the long explanation. </p>
  
  <p>Why is it that even though I'm checking if it's <code>NULL</code> before using the value, it errors out at the part that doesn't get called unless it's not <code>NULL</code>? Does it have to do with the fact that I'm using the <code>IIF()</code> and it's evaluating the whole statement? Or, using the <code>dr()</code>, it evaluates at runtime?</p>
  
  <p>I'm just stumped and would like to know what exactly is going on, so if it's possible, come up with a cleaner solution.</p>
  
  <p>Thanks in advance!</p>