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="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>LINQ Basics Demo</title>

</head>

<body>

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

    <div>

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

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

        </asp:DropDownList>

    </div>

    </form>

</body>

</html>

 

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

        FillListBox(db);

    }

    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";

        ListBox1.DataBind();

 

       //Bind the DropDownList

        DropDownList1.DataSource = products;

        DropDownList1.DataTextField = "ProductName";

        DropDownList1.DataValueField = "ProductID";

        DropDownList1.DataBind();

 

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            ShowProducts();

        }

    }

}

 

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

Technorati Tags: ,,