Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have one dataset with multiple datatables.I am selecting particular datatable from the dataset conditionally by passing table name.I need to select particular columns from that datatable.I am using below code:
dtSelectedColumns = dtSelectedTable.DefaultView.ToTable(true, arrayFields);


Here arrayFields is the string array and this is working fine.But the problem is that,for some situations ,some columns are not there in that datatable.In such scenario,I am getting error like this:
"
C#
Column "column Name" does not belong to underlying table "tableName";.

So how can i check columns are present or not in datatable when selecting particular columns only.Awaiting your fast response
Regards
Aradhana

What I have tried:

I didnt get any proper solution.I tried with linq.But did nt work.
Posted
Updated 1-Mar-16 22:03pm

1 solution

Hi, just refer this code,
C#
public bool CheckColumns(DataTable objTable, string[] columns)
{
    bool IsAllCoumnsExists = false;

    List<string> ColumnList = new List<string>();
    foreach (DataColumn objColumn in objTable.Columns)
    {
        ColumnList.Add(objColumn.ColumnName);
    }

    foreach (string column in columns)
    {
        bool temp = ColumnList.Contains(column);
        if (temp == true)
        {
            IsAllCoumnsExists = true;
        }
        else if (temp == false)
        {
            IsAllCoumnsExists = false;
            break;
        }
    }

    return IsAllCoumnsExists;
}

This code checks all whether all columns are exists in datatable or not.
 
Share this answer
 
v2

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