In this article I will explain with an example, how to bind and populate ASP.Net DropDownList control using LINQ to SQL Framework (dbml classes) using C# and VB.Net.
This article covers the basics of adding and generating dbml classes, connecting to database, adding Table entities and Stored Procedures.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net DropDownList which will be populated from database using LINQ to SQL Framework (dbml classes).
<asp:DropDownList ID = "ddlCustomers" runat="server">
</asp:DropDownList>
 
 
Configuring and connecting LINQto SQL Framework to database
Now I will explain the steps to configure and add LINQ to SQL Framework and also how to connect it with the database.
You will need to add LINQ to SQL classes to your project using Add New Item Dialog as shown below.
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
Now we need to connect to the database and hence you need to right click Data Connections node in Server Explorer and click Add Connection.
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
The above step will open up a Dialog in which you will need to select the Data Source, in our case it is SQL Server and then click Continue.
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
In the next dialog you will need to provide the details of the SQL Server i.e. Server Name and authentication details and then select the database i.e. Northwind.
And then click Test Connection to make sure all settings are correct and then click OK.
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
Now you’ll be able to see the LINQ to SQL (dbml) class created in the App_Code folder of Solution explorer.
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
And also the Northwind database is available in the Server Explorer.
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
Now from here you will need to Drag and Drop the Customers Table to the LINQ to SQL Framework (dbml) class and the save it.
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
That’s it and we are now ready to use the LINQ to SQL Framework.
 
 
Binding the DropDownList using LINQ to SQL Framework
Inside the Page Load event of the page, the DropDownList is populated with the records of the Customers Table.
The CustomerId and the ContactName column values are fetched from the database using LINQ to SQL Framework (dbml classes) and are assigned to the DataTextField and DataValueField properties of the DropDownList control.
DataTextField – The values of the Column set as DataTextField are visible to the user.
DataValueField – The values of the Column set as DataValueField are not visible to the user. Generally ID or Primary Key columns are set as values in order to uniquely identify a DropDownList Item.
Once the records from database are populated, a default item is inserted at the first position.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        CustomersDataContext ctx = new CustomersDataContext();
        ddlCustomers.DataSource = ctx.Customers;
        ddlCustomers.DataTextField = "ContactName";
        ddlCustomers.DataValueField = "CustomerId";
        ddlCustomers.DataBind();
        ddlCustomers.Items.Insert(0, new ListItem("--Select Customer--", ""));
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim ctx As New CustomersDataContext()
        ddlCustomers.DataSource = ctx.Customers
        ddlCustomers.DataTextField = "ContactName"
        ddlCustomers.DataValueField = "CustomerId"
        ddlCustomers.DataBind()
        ddlCustomers.Items.Insert(0, New ListItem("--Select Customer--", ""))
    End If
End Sub
 
 
Screenshot
Populate (Bind) DropDownList using LINQ to SQL in ASP.Net using C# and VB.Net
 
 
Downloads