connect to a database (question)

C

Claudia Fong

HI,


I'm trying to connect to a db using ADO.NET in C#, but it generate
error.. I want to display in a combobox the values of one of the field.
(department name)

I'm new in C#, so I don't know what happen to the code below:


OleDbConnection objConnection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\\db1.mdb");

OleDbCommand myCommand = new OleDbCommand("select DEPT_NAME from
DEPT__2004 order by DEPT_NAME", objConnection);

try
{
objConnection.Open();
OleDbDataReader objReader = myCommand.ExecuteReader();





// fill ComboBox with department name
while ( objReader.Read() )
{
comboBox2.Items.Add(
objReader[ "DEPT_NAME" ] );
}

objConnection.Close(); // close database connection



}
catch (Exception ex)
{
MessageBox.Show("Failed");
}
finally
{
objConnection.Close();
}


Cheers!

Claudi
 
A

ASP.Net programmer

HI,


I'm trying to connect to a db using ADO.NET in C#, but it generate
error.. I want to display in a combobox the values of one of the
field. (department name)

I'm new in C#, so I don't know what happen to the code below:
[...]

It would help if you posted the error message. :)
 
N

Nick Weekes

....and it would also help if you knew the statement producing the
exception (by stepping into the code F11)...

Cheers,

Nick
 
S

Søren Reinke

And replace:
MessageBox.Show("Failed");

with
MessageBox.Show(ex.Message);

Gives you a little idea what might be wrong.

Not good in the final compilation, but for development i always have it on.
 
C

Claudia Fong

Hi,

Thanks, after using the code that you suggest I found out that I
"misspell" the tables's name :~)


MessageBox.Show(ex.Message);


Thanks again!

Claudi
 

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

Top