Hi akhter,
Please refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:DropDownList ID="ddlCountries" runat="server"></asp:DropDownList>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId, CONCAT(FirstName, ' ', EmployeeId) Name FROM Employees", con))
{
con.Open();
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "EmployeeId";
ddlCountries.DataSource = cmd.ExecuteReader();
ddlCountries.DataBind();
con.Close();
ddlCountries.Items.Insert(0, new ListItem("Please select", "0"));
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeId, CONCAT(FirstName, ' ', EmployeeId) Name FROM Employees", con)
con.Open()
ddlCountries.DataTextField = "Name"
ddlCountries.DataValueField = "EmployeeId"
ddlCountries.DataSource = cmd.ExecuteReader()
ddlCountries.DataBind()
con.Close()
ddlCountries.Items.Insert(0, New ListItem("Please select", "0"))
End Using
End Using
End Sub
Screenshot