Dwalden
asked on
DropDownList control filled with "System.Data.Common.DbDataRecord" on Page_Load
Howdy everyone.
What I'm trying to do is populate a DropDownList control in a web form with values from an MS Access database when the webform loads. When I browse the page, all the items in the control say "System.Data.Common.DbData Record".
In the Access database is the table "tblStates". This table has 2 fields only:
ID - an autonumber set as the key
States - a text field containing the states in the US.
Here is the code from the Page_Load event:
private void Page_Load(object sender, System.EventArgs e)
{
string sql = @"SELECT ID, States from tblStates ORDER BY ID";
string conStr =@"Provider=Microsoft.Jet. OLEDB.4.0; Data Source=C:\databases\cybers late.mdb;P ersist Security Info=False";
OleDbConnection dcStates = null;
try
{
dcStates = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sql, dcStates);
cmd.CommandTimeout = 30;
dcStates.Open();
OleDbDataReader drStates = cmd.ExecuteReader(CommandB ehavior.Cl oseConnect ion);
ddlTest.DataSource = drStates;
ddlTest.DataBind();
ddlTest.Items.Insert(0, new ListItem("Select State"));
}
catch (Exception exc)
{
throw exc;
}
finally
{
dcStates.Close();
}
}
What am I doing wrong? BTW, I'm just now learning C# so I'm not using the toolbox in VS.NET until I have a firm grasp on doing things manually. I'd appreciate any help that doesn't suggest using the toolbox :)
What I'm trying to do is populate a DropDownList control in a web form with values from an MS Access database when the webform loads. When I browse the page, all the items in the control say "System.Data.Common.DbData
In the Access database is the table "tblStates". This table has 2 fields only:
ID - an autonumber set as the key
States - a text field containing the states in the US.
Here is the code from the Page_Load event:
private void Page_Load(object sender, System.EventArgs e)
{
string sql = @"SELECT ID, States from tblStates ORDER BY ID";
string conStr =@"Provider=Microsoft.Jet.
OleDbConnection dcStates = null;
try
{
dcStates = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sql, dcStates);
cmd.CommandTimeout = 30;
dcStates.Open();
OleDbDataReader drStates = cmd.ExecuteReader(CommandB
ddlTest.DataSource = drStates;
ddlTest.DataBind();
ddlTest.Items.Insert(0, new ListItem("Select State"));
}
catch (Exception exc)
{
throw exc;
}
finally
{
dcStates.Close();
}
}
What am I doing wrong? BTW, I'm just now learning C# so I'm not using the toolbox in VS.NET until I have a firm grasp on doing things manually. I'd appreciate any help that doesn't suggest using the toolbox :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot