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