C#
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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Dear All
string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=School.accdb";
OleDbConnection connection = new OleDbConnection(connstr );
connection.Open();
string sql = "select * from Student";
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connection);
DataSet set=new DataSet();
adapter.Fill(set);
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?
Thanks for your reply. See the screen capture below for the error.
@BenTam-3003 , Based on my test, the latest code you provided still works for me. I think that maybe your sql query is wrong.
Could you have a check like the following picture to check if your Dataset is null?
Hi @Jack J Jun
Yes it is null.
Hi @Jack J Jun
I know the issue now. It is because the DataSet is null. Now the problem is solved.
By the way, how can I get the DataSet Visualizer?
It's part of the Visual Studio debugger and accessed exactly as shown in the animated gif.
Hi @AgaveJoe
Thanks for your tip. I found it.
Sign in to comment
2 answers
Sort by: Most helpful
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.
Hi @Ken Tucker
I have made sure that the statement returns "1", i.e., the starting StudentID, in the SSMS.
I have always looked at the data in an access database in access. I would just double check you are looking at the same database as your app is
Sign in to comment
Your latest code returns a single column "ID" but the LINQ query is looking for "Age".
Hi @AgaveJoe
Thanks for your reply. It is because my project is a student registration system and his reply is only a sample.
Sign in to comment
Activity