user824910
user824910

Reputation: 1117

Column <column name> does not belong to table <table name>

I have a method working with dataset,

 var EmpNameList = new List<string>();
 var EmpDS = GetEmployeeList();
 foreach (DataRow EmpDR in EmpDS.Tables[0].Rows)
 {
     EmpNameList.Add(EmpDR["EmpName"].ToString()); // Error on this line
 }

exception Details:

Column 'EmpName' does not belong to table 'EmpDetailTbl' at System.Data.DataRow.GetDataColumn(String columnName) at System.Data.DataRow.get_Item(String columnName) at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)

at times we also get the following exception for the same method

Cannot find table 0 at System.Data.DataTableCollection.get_Item(Int32 index)

All the above exception are not reproduced consistently and it is intermittent.

The GetEmployeeList definition looks as below

  public DataSet GetEmployeeList()
  {
    var  Connectionstring = "MyConnectionString";
    var query = "Select EmpName, EmpId, HireDate from EmpDetail"; 
    DataSet ds = new DataSet();

    using (OleDbConnection connection = new OleDbConnection(Connectionstring))
    using (OleDbCommand command = new OleDbCommand(query, connection))
    using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
    {
      adapter.Fill(ds,"EmpDetailTbl");
      return ds;
    }
 }

I have tried with SQL Server using the SqlConnection class also.

This seems to be wierd huh???

Upvotes: 0

Views: 16991

Answers (2)

Jack B
Jack B

Reputation: 149

http://msdn.microsoft.com/en-us/library/y4b211hz(v=vs.110).aspx

Check out the remarks - if the actual query string you're using returns no rows, no tables are added to the dataset. Also note that the strings used are case sensitive.

Upvotes: 0

Andy G
Andy G

Reputation: 19367

If you are filling other DataTables in the DataSet then "EmpDetailTbl" may not be the first one every time (0). Refer to the DataTable by name:

foreach (DataRow EmpDR in EmpDS.Tables["EmpDetailTbl"].Rows) { 

Upvotes: 3

Related Questions