In this article I will explain how to add (insert) Default Item (Initial Value) to DataBound DropDownList populated from Database in ASP.Net Code Behind using C# and VB.Net.
Generally we want to have a default selected item such as Please Select. This article will explain how to add such default item in Code Behind when DropDownList is populated from database.
 
Database
I have made use of the following table Customers with the schema as follows.
Set Default value in ASP.Net DropDownList in Code Behind using C# and VB.Net
I have already inserted few records in the table.
Set Default value in ASP.Net DropDownList in Code Behind using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup consists of an ASP.Net DropDownList which will be populated from database.
<asp:DropDownList ID = "ddlCustomers" runat="server">
</asp:DropDownList>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Populating DropDownList control from database and setting Default value in ASP.Net
Inside the Page Load event of the page, the DropDownList is populated with the records of the Customers Table.
The CustomerId and the Name column values are fetched from the database using SqlDataReader 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)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name FROM Customers"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                ddlCustomers.DataSource = cmd.ExecuteReader();
                ddlCustomers.DataTextField = "Name";
                ddlCustomers.DataValueField = "CustomerId";
                ddlCustomers.DataBind();
                con.Close();
            }
        }
        ddlCustomers.Items.Insert(0, new ListItem("--Select Customer--", "0"));
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("SELECT CustomerId, Name FROM Customers")
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                con.Open()
                ddlCustomers.DataSource = cmd.ExecuteReader()
                ddlCustomers.DataTextField = "Name"
                ddlCustomers.DataValueField = "CustomerId"
                ddlCustomers.DataBind()
                con.Close()
            End Using
        End Using
        ddlCustomers.Items.Insert(0, New ListItem("--Select Customer--", "0"))
    End If
End Sub
 
Set Default value in ASP.Net DropDownList in Code Behind using C# and VB.Net.
 
Demo
 
Downloads