In this article I will explain with an example, how to add item at Index 0 (First position) in ASP.Net DropDownList that already contains items (data) using C# and VB.Net.
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
The HTML Markup consists of an ASP.Net DropDownList which will be populated from database.
<asp:DropDownList ID = "ddlCustomers" runat="server">
You will need to import the following namespaces.
Populating DropDownList control from database and adding item at Index 0 (First position)
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 blank (empty) item is inserted at the first position.
protected void Page_Load(object sender, EventArgs e)
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;
ddlCustomers.DataSource = cmd.ExecuteReader();
ddlCustomers.DataTextField = "Name";
ddlCustomers.DataValueField = "CustomerId";
//Add blank item at index 0.
ddlCustomers.Items.Insert(0, new ListItem("", ""));
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
ddlCustomers.DataSource = cmd.ExecuteReader()
ddlCustomers.DataTextField = "Name"
ddlCustomers.DataValueField = "CustomerId"
'Add blank item at index 0.
ddlCustomers.Items.Insert(0, New ListItem("", ""))