Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check if a column exists in the datareader before attempting to read it
i try some of code below
C#
string ColumnValue;

if (dr["ColumnName"] != null)
      ColumnValue = dr["ColumnName"].ToString();

if (dr.GetSchemaTable().Columns["ColumnName"] != null)
      ColumnValue = dr["ColumnName"].ToString();

if (dr.GetSchemaTable().Columns.IndexOf("ColumnName")> 0)
      ColumnValue = dr["ColumnName"].ToString();

but its not work. any idea about this.
Thanks
Posted
Updated 20-Jan-21 1:15am
v4

looping will give u a solution of problem.
loop through the all columns name and check if your column is exist or not.

C#
for (int i=0; i < dr.FieldCount; i++)
 {
            
         if (dr.GetName(i).Equals(columnName,StringComparison.InvariantCultureIgnoreCase))
          return true;
 }
return false; 
 
Share this answer
 
Comments
AditSheth 19-Sep-11 8:37am    
thanks kool prasad
My vote is 5
 
Share this answer
 
Comments
AditSheth 19-Sep-11 8:37am    
Thanks Anuja
My vote is 5
Anuja Pawar Indore 19-Sep-11 8:54am    
Welcome adit :)
I'd stick with the GetSchemaTable() method as the MSDN documentation explains:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getschematable.aspx[^]

maybe there's a typo or two in your sample:

C#
string ColumnName;

if (dr[ColumnName] != null)
      ColumnName = dr[ColumnName].ToString();

if (dr.GetSchemaTable().Columns[ColumnName] != null)
      ColumnName = dr[ColumnName].ToString();

if (dr.GetSchemaTable().Columns.IndexOf(ColumnName)> 0)
      ColumnName = dr[ColumnName].ToString();


try removing the quotes around ColumnName so you're using the value of ColumnName and not the literal "ColumnName".
 
Share this answer
 
v2
Comments
AditSheth 19-Sep-11 8:35am    
ohh Jim sorry
i thing you are not getting my question because of i take a result as ColumnName.
Consider String ColumnName is a result of datareader["ColumnName"]
Let i update Question..
Thanks
jim lahey 19-Sep-11 8:45am    
I get you now.
you can do dr["ColumnNameColumnName"] for example.. however, normally i do it in a different way like so

if (dr["ColumnName"] != null)
{

int ColumnName_index = dr.GetOrdinal("ColumnName");
String ColumnName= dr.GetString(ColumnName_index));
}
 
Share this answer
 
Comments
AditSheth 19-Sep-11 8:31am    
I already say in example that if (dr["ColumnName"] != null) is false although i have a column in datareader.
var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();  

columns.Any(s => s == columnName)?true:false;
 
Share this answer
 
Comments
Richard Deeming 20-Jan-21 12:28pm    
This question was solved in the same year it was posted. Your solution is horrendously inefficient.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900