ds.Tables[0] causes error

BenTam-3003 681 Reputation points

Dear All

string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=School.accdb";
     OleDbConnection connection = new OleDbConnection(connstr );
     string sql = "select * from Student";
     OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connection);
     DataSet set=new DataSet();  
     var table=set.Tables[0];
     var result = table.AsEnumerable().Select(x => x.Field<int>("Age")).Take(1);
     Console.WriteLine("First student Age is "+result.First());

In the above code, the line "ds.Tables[0]" causes error. Could anybody help me?

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,498 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ken Tucker 5,811 Reputation points

    The error is because query is not returning any results. I would check that the database is making it to the bin folder of your app when debugging. I would also verify the table name.

  2. AgaveJoe 19,441 Reputation points

    Your latest code returns a single column "ID" but the LINQ query is looking for "Age".