IIF Evaluates Both Cases

Forgot about this little wtf with IIF() in vb.net.  It will evaluate both cases.  Another reason why I like c# better than vb.net. The following snippet will throw an "Invalid Cast Exception cannot convert DbNull to String" when the value of that cell is DbNull.  Even though you would think it would just return Nothing in that case.

                sIncident.ExpireDate = IIf(row.Cells("colExpireDate").Value Is DBNull.Value, Nothing, DateTime.Parse(row.Cells("colExpireDate").Value))

So an alternative is just to use If ..else

 If (row.Cells("colExpireDate").Value Is DBNull.Value) Then
                    sIncident.ExpireDate = Nothing
                Else
                    sIncident.ExpireDate = DateTime.Parse(row.Cells("colExpireDate").Value)
                End If

Comments

Popular posts from this blog

Asp.net Publishing Broke Site - "App_WebReferences is not allowed because the application is precompiled"

Telerik - Custom Group Footers In RadGrid

Bootstrap Modal Popup is Grayed Out