Search code examples
.netvisual-studiodatareader

Check for existence of column in DataReader OR not make debugger break on certain exceptions


I have code that looks like:

  //System.Data.IDataRecord dr
  try
  {
       Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
  }
  catch (IndexOutOfRangeException) { } //swallow

I don't know if the consolidated column will be present in the datareader, so I do that to check. It works fine (is a little hackish, though).

When I attach a debugger though, it breaks on that whenever it throws the exception however. Extremely annoying.

Is there a better way to write that code; or is there some Visual Studio way of telling it to ignore the exception and not break (but only right here; not everywhere).


Solution

  • Yes, you can use the GetSchemaTable() method of the datareader to get a list of columns, then you can see if that column exists.

    You might find this very similar question helpful.