Bind ASP.Net Dropdownlist using Linq to SQL Query in C# and VB.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

Hi all,

in my Solution i have Test.DL & Test.Web.UI.

i am bind the linqtosql.dbml in Test.DL.

under Test.DL i have Ddlfile.cs in this file i have to write linq queries.

under Test.Web.UI i have Ddlfile.ascx file.

in this file i want to connect linq queries.

in my query,

in Ddlfile.ascx file i have one dropdownlist and i want to bind this dropdown list in page load.

and also i write the linq query in .ascx file .

i tried below query and executed successfully.

using (TestDataContext TDC = new TestDataContext())
    dropdownlist.DataSource = (from ddlvalue in TDC.Table1 orderby ddlvalue.Id select new { ddlvalue.Id, ddlvalue.Name });  //i want to retrieve this query from Ddlfile.cs

    dropdownlist.DataTextField = "Name";
    dropdownlist.DataValueField = "Id";
    dropdownlist.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Option", "0"));

but asper my requirement i want to write the linq query under Ddlfile.cs and fetch the this query to Ddlfile.ascx.

How to do this..

if you understand my problem plese help me..

Thanks in advance..

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago Modified on 2 months ago

Hi nagaraju60,

Check this example. Now please take its reference and correct your code.

For this example i have refered the below article for connecting with database.

Populate (Bind) GridView using LINQ to SQL in ASP.Net using C# and VB.Net


Here I am making use of Microsoft’s Northwind Database Categories Table. You can download it from here.

Download and install Northwind Database


public List<DropDownListItem> GetDropDownList()
    List<DropDownListItem> items = new List<DropDownListItem>();
    using (CategoryDataContext TDC = new CategoryDataContext())
        items = (from ddlvalue in TDC.Categories
                    orderby ddlvalue.CategoryID
                    select new DropDownListItem
                        Id = ddlvalue.CategoryID,
                        Name = ddlvalue.CategoryName

    return items;

public class DropDownListItem
    public string Name { get; set; }
    public int Id { get; set; }


Public Function GetDropDownList() As List(Of DropDownListItem)
    Dim items As List(Of DropDownListItem) = New List(Of DropDownListItem)()
    Using TDC As CategoryDataContext = New CategoryDataContext()
        items =(From ddlvalue In TDC.Categories Order By ddlvalue.CategoryID Select New DropDownListItem With {.Id = ddlvalue.CategoryID, .Name = ddlvalue.CategoryName}).ToList()
    End Using

    Return items
End Function

Public Class DropDownListItem

    Public Property Name As String

    Public Property Id As Integer
End Class


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
    Inherits="WebUserControl" %>
<asp:DropDownList runat="server" ID="dropdownlist">


protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        using (CategoryDataContext TDC = new CategoryDataContext())
            Ddlfile file = new Ddlfile();
            dropdownlist.DataSource = file.GetDropDownList();
            dropdownlist.DataTextField = "Name";
            dropdownlist.DataValueField = "Id";
            dropdownlist.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Option", "0"));


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Using TDC As CategoryDataContext = New CategoryDataContext()
            Dim file As Ddlfile = New Ddlfile()
            dropdownlist.DataSource = file.GetDropDownList()
            dropdownlist.DataTextField = "Name"
            dropdownlist.DataValueField = "Id"
            dropdownlist.Items.Insert(0, New System.Web.UI.WebControls.ListItem("Select Option", "0"))
        End Using
    End If
End Sub

Then access the UserControl in the page.


<%@ Register Src="~/WebUserControl.ascx" TagName="Category" TagPrefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <uc:Category ID="ucCategory" runat="server" />


I agree, here is the link: