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".
1 Answer
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;
}
1 Comment
user3106445
I have already used
GetName but still getting the same error.