Binding data to a DropDownList with ADO.NET

4.67 (3 votes)

Using ADO.NET to pull data from the Northwind database (Access version), using the saved query Current Product List.

Continuing with my preference for using saved queries in Access, for very much the same reasons that I prefer stored procedures in SQL Server, here is a straightforward example of calling "Current Product List" from the Northwind database and binding it programmatically to a DropDownList.

Two things of note are that the name of the query is surrounded with [ ] brackets, to get over the ill-advised use of embedded spaces in Access object names, and secondly, calling a saved query in Access requires that the CommandType property is set to StoredProcedure.

string ConnString = Utils.GetConnString();
string SqlString = "[Current Product List]";
using (OleDbConnection conn = new OleDbConnection(ConnString ))
{	
  using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
  {
    cmd.CommandType = CommandType.StoredProcedure;
    conn.Open();
    OleDbDataReader rd = cmd.ExecuteReader();
    while (rd.Read())
    {
      DropDownList1.DataSource = rd;
      DropDownList1.DataValueField = "ProductID";
      DropDownList1.DataTextField = "ProductName";
      DropDownList1.DataBind();
    }
  }
}

Date Posted:
Last Updated:
Posted by:
Total Views to date: 15461

0 Comments

Recent Comments

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....