Had an interesting problem today in that I wanted to see if a specific column was returned in a datareader. The query only returned fields if a user had entered the data, so I was trying to access a column that often times wasn't available in the dataset. The reason behind it was some what convulted, but below is a nice solution I found digging around some blogs.
Public Function ValidColumn(ByVal strColumnName As String, ByVal dr As SqlDataReader) As Boolean
dr.GetSchemaTable().DefaultView.RowFilter = "ColumnName= '" + strColumnName + "'"
Return (dr.GetSchemaTable().DefaultView.Count > 0)
End Function
The only other solution I found was to use a try/catch and determine if the error occured and then using the trap for the result, which I found a bad way to go.