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
Database
Here I am making use of Microsoft’s Northwind Database Categories Table. You can download it from here.
Download and install Northwind Database
Ddlfile.cs
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
}).ToList();
}
return items;
}
public class DropDownListItem
{
public string Name { get; set; }
public int Id { get; set; }
}
Ddlfile.vb
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
UserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %>
<asp:DropDownList runat="server" ID="dropdownlist">
</asp:DropDownList>
UserControl.ascx.cs
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.DataBind();
dropdownlist.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Option", "0"));
}
}
}
UserControl.ascx.vb
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.DataBind()
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.
HTML
<%@ Register Src="~/WebUserControl.ascx" TagName="Category" TagPrefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:Category ID="ucCategory" runat="server" />
</div>
</form>
</body>
</html>
Screenshot
![](https://i.imgur.com/aKZnEX7.jpg)