avoid checking for DataRow.IsDBNull on each column?

前端 未结 4 1813
无人及你
无人及你 2021-01-19 07:46

My code is 2x longer than it would be if I could automatically set IsDBNull to \"\" or simply roll over it without an error.

This is my cod

4条回答
  •  野的像风
    2021-01-19 08:19

    Ceres's answer is probably the best given that it avoids any sort of null testing, but it's worth noting that the 'IIF' function would also work pretty well her. It's still going to do the test for null but it's much more compact than how Joe was originally doing it. Something like this should do the trick:

    For Each rs As DataRow In sqlDataset.Tables(0).Rows
    
        Response.Write( IIF( IsDBNull(rs("column")), "", rs("column") ) )
    
    Next
    

    What's neat with this is you can substitute the "" for whatever you want to output if the value is in fact null ( a nice little added bonus. )

    Here's some info on the 'IIF' function for those who don't know what it is:

    http://msdn.microsoft.com/en-ca/library/27ydhh0d(v=vs.71).aspx

提交回复
热议问题