/ Published in: C#
This snippet is "extension method" for IDataReader.
You can use this snippet below ;
I assume you have datareader like that
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.IsColumnExists("columnName"))
{
//do something
}
and also you should validation null value control if you work critical datas
if (dataReader.IsColumnExists("columName"))
{
if (dataReader["columName"] != DBNull.Value)
{
//do something
}
}
You can use this snippet below ;
I assume you have datareader like that
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.IsColumnExists("columnName"))
{
//do something
}
and also you should validation null value control if you work critical datas
if (dataReader.IsColumnExists("columName"))
{
if (dataReader["columName"] != DBNull.Value)
{
//do something
}
}
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public static bool IsColumnExists(this IDataReader dataReader, string columnName) { bool retVal = false; try { dataReader.GetSchemaTable().DefaultView.RowFilter = string.Format("ColumnName= '{0}'", columnName); if (dataReader.GetSchemaTable().DefaultView.Count > 0) { retVal = true; } } catch (Exception ex) { throw; } return retVal; }