Geeks With Blogs


Microsoft MVP


Moderator at CodeASP.NET

Quiz Master

free counters
Free counters
Added on January 19,2012

Follow Me @vmsdurano

A bit About Me

The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.

Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example shows the basic way on how to populate a data from database to a ListBox/DropDownList control using LINQ to SQL.

Note that I used Northwind database here just for the simplicity of this example.

To get started then let’s go ahead and add a new item in your website application in Visual Studio 2008 or VWD Express 2008. To do this, just right click on the web site -> select Add New Item -> and on the Templates select LINQ to SQL Classes as shown below and click ok:


Now open up Server Exporer in Visual Studio and do the follwing:

* Under Data Connections, expand the northwind database

* Expand the Table folder and select the Table that you want to use and drag it on the Data Context. Note that I used the product table in this example as shown below:



Now add a new webform then grab a ListBox and a DropDownList control from the Visual Studio ToolBox and place it on the ASPX source. The mark up would look something like this:


<html xmlns="">

<head runat="server">

    <title>LINQ Basics Demo</title>



    <form id="form1" runat="server">


        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>

        <asp:DropDownList ID="DropDownList1" runat="server">







And here are the code blocks below for binding the ListBox and DropDownList control using LINQ:


using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;


public partial class LINQDemo : System.Web.UI.Page


    private void ShowProducts()


        //create new instance of the the DataContext class that we have just created

        DataClassesDataContext db = new DataClassesDataContext();



    private void FillListBox(DataClassesDataContext db)


        //LINQ syntax

        //Select the ProductID and ProductName from Products

        var products = from c in db.Products

        select new


            ProductID = c.ProductID,

            ProductName = c.ProductName,



        //Bind the ListBox

        ListBox1.DataSource = products;

        ListBox1.DataTextField = "ProductName";

        ListBox1.DataValueField = "ProductID";



       //Bind the DropDownList

        DropDownList1.DataSource = products;

        DropDownList1.DataTextField = "ProductName";

        DropDownList1.DataValueField = "ProductID";




    protected void Page_Load(object sender, EventArgs e)


        if (!IsPostBack)







As you can see the code above is straight forward and self explanatory. That's it!

Technorati Tags: ,,
Posted on Tuesday, September 1, 2009 6:33 PM ASP.NET , C# , LINQ to SQL | Back to top

Comments on this post: BASIC: Binding ListBox and DropDownList using LINQ

# re: BASIC: Binding ListBox and DropDownList using LINQ
Requesting Gravatar...
Man, YOU R MY SAVIOUR...I just started working with LINQ in from the beginning, i was not able to add DataClassesDataContext and i was struggling....and theni saw ur blog...WOW good work for basic programmer like my dropdown list binded....

but i have a litte doubt tht i am curious in asking
y did u use "new" in the query
select new
ProductID = c.ProductID,
ProductName = c.ProductName,
and also a semi colon....i just used select to get my coloumn.....

Thanks a lot for u help...Is there more on LINQ so that we can learn more....
Left by Bharathi on Sep 07, 2009 12:25 AM

# re: BASIC: Binding ListBox and DropDownList using LINQ
Requesting Gravatar...
Tmaro Khub Khub AABHAAR.....
Left by Rajat on Mar 08, 2010 6:10 PM

# re: BASIC: Binding ListBox and DropDownList using LINQ
Requesting Gravatar...
I able to get
DataClassesDataContext db = new DataClassesDataContext();

Please explain about the class
Left by venkat on Sep 10, 2011 1:21 AM

# re: BASIC: Binding ListBox and DropDownList using LINQ
Requesting Gravatar...
Really great help
Left by Deepali on Jun 21, 2012 8:08 PM

# re: BASIC: Binding ListBox and DropDownList using LINQ
Requesting Gravatar...
Quick question how can I send the selected item on the list box to populate a multiple text boxes.

Left by Marv on Dec 23, 2012 5:37 AM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: