Detecting If a Field Is Present in a DataReader

Posted: Wednesday, April 18, 2007 2:25:20 PM | 1408 Views


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.




User Comments

No Comments Yet!


Leave a Comment

*Your Name:

*Email (will not be shown):

Website (optional):

*Comments:

NOTE: HTML is stripped from comments.

  JmrqJ  
*Enter code above:






(c) 2009 Mark Hansen aka "stonedonkey" ~ Powered By: donkeyblogger - 0.016 secs