I have to check a column if it doesn't exists in SqlDataReader
. I tried using reader.IsDBNull
and reader.GetOrdinal
, but still getting error "Index Out of range".
Asked
Active
Viewed 2,187 times
0

user3106445
- 341
- 1
- 4
- 13
1 Answers
0
use this method
public static class DataRecordExtensions
{
public static bool HasColumn(this IDataRecord dr, string columnName)
{
for (int i=0; i < dr.FieldCount; i++)
{
if (dr.GetName(i).Equals(columnName, StringComparison.InvariantCultureIgnoreCase))
return true;
}
return false;
}
}
try this
public static bool HasColumn(DbDataReader Reader, string ColumnName) {
foreach (DataRow row in Reader.GetSchemaTable().Rows) {
if (row["ColumnName"].ToString() == ColumnName)
return true;
} //Still here? Column not found.
return false;
}

Rohit Poudel
- 1,793
- 2
- 20
- 24
-
I have already used `GetName` but still getting the same error. – user3106445 Jun 12 '17 at 06:17