Link to home
Start Free TrialLog in
Avatar of Dwalden
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.DbDataRecord".

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\cyberslate.mdb;Persist 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(CommandBehavior.CloseConnection);

          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
Avatar of wiseguy_2112
wiseguy_2112

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dwalden
Dwalden

ASKER

Got it in one. I wasn't setting the DataTextField and DataValueField.

Thanks a lot