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();
    }
  }
}

You might also like...

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

0 Comments

Recent Comments

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...

Mau 06/06/2017 08:58
In response to Razor Pages - The Elevator Pitch
I am missing the razor view code. Thank you for your article. Design pattern should improve the of...

Obinna Okafor 26/05/2017 16:16
In response to Razor Pages - The Elevator Pitch
Thank you for this wonderful piece, Mike. I need more of these...Keep them coming - anything more...

Cyrus 26/05/2017 06:00
In response to Razor Pages - The Elevator Pitch
There are some concern about razor pages performance. Is it faster or slower than MVC? would you a a...

Cyrus 26/05/2017 05:44
In response to Razor Pages - Understanding Handler Methods
well done, thank you....

Satyabrata Mohapatra 23/05/2017 11:41
In response to Razor Pages - Understanding Handler Methods
Nice and easy !! Great post....

Cyrus 16/05/2017 19:55
In response to Razor Pages - Getting Started With The Preview
There is something wrong related to microsoft.dotnetcore.mvc.taghelpers! if you remove it from page...

Cyrus 16/05/2017 10:18
In response to Razor Pages - Getting Started With The Preview
well done mike, it was very useful, I really appreciate that....

Satyabrata Mohapatra 16/05/2017 07:21
In response to Razor Pages - Getting Started With The Preview
Finally!!!! web pages in asp.net core!!! Super excited !!!! Thank u sir for sharing.....Awaiting on...

Daniele 14/03/2017 10:24
In response to Working With Zip Files In ASP.NET MVC
is it possible give to the user a progress bar of the zipping process? Thanks in advance. ...