Error Accessing ACCESS.MDB

S

Shayer

Hello All,
I write the following code to access Data from a table
But i get the exception "Syntax Error in From Clause"

when i write the code in SQL then it works perfectly.

OleDbConnection Myconnection = null;

OleDbDataReader dbReader = null;

Myconnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0; User
Id=; Password=; Data Source=C:\STTS.mdb");

Myconnection.Open();

OleDbCommand cmd = Myconnection.CreateCommand();

cmd.CommandText = "SELECT * FROM user";

dbReader = cmd.ExecuteReader();

string Email;

while(dbReader.Read())

{

Email = (string)dbReader.GetValue(0);

lb.Items.Add(Email);

}

dbReader.Close();

Myconnection.Close();



Can anybody tell me where is the error

Thanks
 
D

dip

try setting your CommandText as :
cmd.CommandText = "select * from [user]"

- dip
-----Original Message-----
Hello All,
I write the following code to access Data from a table
But i get the exception "Syntax Error in From Clause"

when i write the code in SQL then it works perfectly.

OleDbConnection Myconnection = null;

OleDbDataReader dbReader = null;

Myconnection = new OleDbConnection
(@"Provider=Microsoft.Jet.OLEDB.4.0; User
 
S

Shayer

Hi Dip

thanks for ur reply. Yap i think it will work.
I already change the table name and try and it works. i think that because
of table name which might be an key word.

THanks

dip said:
try setting your CommandText as :
cmd.CommandText = "select * from [user]"

- dip
-----Original Message-----
Hello All,
I write the following code to access Data from a table
But i get the exception "Syntax Error in From Clause"

when i write the code in SQL then it works perfectly.

OleDbConnection Myconnection = null;

OleDbDataReader dbReader = null;

Myconnection = new OleDbConnection
(@"Provider=Microsoft.Jet.OLEDB.4.0; User
Id=; Password=; Data Source=C:\STTS.mdb");

Myconnection.Open();

OleDbCommand cmd = Myconnection.CreateCommand();

cmd.CommandText = "SELECT * FROM user";

dbReader = cmd.ExecuteReader();

string Email;

while(dbReader.Read())

{

Email = (string)dbReader.GetValue(0);

lb.Items.Add(Email);

}

dbReader.Close();

Myconnection.Close();



Can anybody tell me where is the error

Thanks


.
 
P

Paul Clement

¤ Hello All,
¤ I write the following code to access Data from a table
¤ But i get the exception "Syntax Error in From Clause"
¤
¤ when i write the code in SQL then it works perfectly.
¤
¤ OleDbConnection Myconnection = null;
¤
¤ OleDbDataReader dbReader = null;
¤
¤ Myconnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0; User
¤ Id=; Password=; Data Source=C:\STTS.mdb");
¤
¤ Myconnection.Open();
¤
¤ OleDbCommand cmd = Myconnection.CreateCommand();
¤
¤ cmd.CommandText = "SELECT * FROM user";
¤

The field name "User" is an SQL ODBC reserved word. Use dip's suggestion to work around the issue.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top