Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!!

Posted

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.

Gamer extraordinaire. Programmer wannabe.
  • Administrators
Posted

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();
		}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...