Error Accessing ACCESS.MDB

  • Thread starter Thread starter Shayer
  • Start date Start date
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
 
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
 
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


.
 
¤ 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)
 
Back
Top