ajaykothidar Posted October 14, 2003 Posted October 14, 2003 Hi, I am trying to read and write into Access database. I have defined a class as below public class dataReader{ public void EnumerateEmployees( string fileName){ OleDbConnection cnctn = new OleDbConnection(); cnctn.ConnectionString= "Provider=Microsoft.JET.OLEDB.4.0;" + "data source=" + fileName; IDataReader rdr = null; try { cnctn.Open(); IDbCommand sel = new OleDbCommand( "select * from employees", cnctn); rdr = sel.ExecuteReader(); while (rdr.Read()) { System.Console.WriteLine( rdr["Name"] + " " + rdr["age"]); } } finally { rdr.Close(); cnctn.Close(); } } } Ans i am calling this function as [sTAThread] static void Main() { dataReader dr= new dataReader(); this.EnumerateEmployees("C:\\C drive\\#ak\\RND\\Sharp\\sharp.mdb"); } I am getting the error as Unhandled Exception: System.Data.OleDb.OleDbException: Syntax error in FROM clau se. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARA MS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Ob ject& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behav ior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader() at FormControlEvent.dataReader.EnumerateEmployees(String fileName) at FormControlEvent.Form1.Main() Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at FormControlEvent.dataReader.EnumerateEmployees(String fileName) at FormControlEvent.Form1.Main() any Comments!! Quote
wyrd Posted October 14, 2003 Posted October 14, 2003 It's a little hard to tell, but it looks like you forgot to instantiate your reader using your command object. If you could use the <cs></cs> blocks (use [ ] instead of < >) to post code it'd be much appreciated. Quote Gamer extraordinaire. Programmer wannabe.
Administrators PlausiblyDamp Posted October 14, 2003 Administrators Posted October 14, 2003 Looks like it could be a problem with the line IDbCommand sel =new OleDbCommand("select * from employees", cnctn); does the cnctn equal a valid object? If not that is probably the problem - even though you've put the code in a try block you haven't got a catch block! If it fails you will still get the unhandled exception message. try changing it to something like the following and see if you get the message box. public void EnumerateEmployees(string fileName) { OleDbConnection cnctn = new OleDbConnection(); cnctn.ConnectionString= "Provider=Microsoft.JET.OLEDB.4.0;data source=" + fileName; IDataReader rdr = null; try { cnctn.Open(); IDbCommand sel =new OleDbCommand("select * from employees", cnctn); rdr = sel.ExecuteReader(); while (rdr.Read()) { System.Console.WriteLine(rdr["Name"] + " "+ rdr["age"]); } } catch { System.Windows.Forms.MessageBox.Show("fluff"); } finally { rdr.Close(); cnctn.Close(); } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ajaykothidar Posted October 15, 2003 Author Posted October 15, 2003 Thanks , It worked with the query--> "select * from employees". There was no problem with instantiation nor with the line of query. For the same code as above it gives error for query--> "select * from user". The reason being user is restricted word in MS-Acess . Due to which it was throwing stream of errors. Regards Ajay Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.