2

I came across situation,where columnname for datatable is dynamic.While fetching a data I want to check existence of column.

DataTable table = ds.Table["Sample1"]

if(table.Row.Count > 0)
{
foreach(DataRow dr in table.Rows )
{
    if(dr.Table.Column.Contain("DateInfo"))
    {
        // store value in variable
        // first approach
    }

    if(table.Column.Contain("DateInfo"))
    {
        // store value in variable
        // second approach
    }
}
}

Which one is best approach?

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Jui Test
  • 2,399
  • 14
  • 49
  • 76

2 Answers2

3

Will this be enough:

1st Approach: Which will simply check in an entire DataTable.

datatable.Columns.Contains("column")

2nd Approach: which will check for each row collection in DataTable

dr.Table.Columns.Contains("column")

3rd Approach: Which fetch each columns in DataColumnCollection object and then check if it contains the specific field or not.

DataColumnCollection columns = datatable.Columns;

if (columns.Contains(columnName))

So these all approaches are better in their own way. you can use whatever you find it better.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
1

This is best one

dr.Table.Column.Contain("DateInfo")

foreach loop get single row at a time sometimes if any conditional is possible in this method

senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36