0

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".

0

1 Answer 1

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; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have already used GetName but still getting the same error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.