ListBox show data from database in

In this article i'm going to show you how to bind data from database into listbox control in

HTML Markup Language - Add ListBox

ListBox control allows single or multiple item selection. To enable multiple item selection, set the SelectionMode property to Multiple.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> 
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>

C# Coding :

C# Coding : Namespace

Adding Namespace which help to Bind data from database to application i.e SqlConnection , SqlCommand , SqlDataAdapter and DataSet .

 using System.Data;
 using System.Data.SqlClient;

C# Coding : BindCountryListBox

protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)

 private void BindCountryListBox()
   SqlConnection conn = new SqlConnection("Data Source=.; Database=master; User ID=sa; Password=12345;");
   SqlDataAdapter da = new SqlDataAdapter("select countryid, countryname from CountryTable", conn);
   DataSet ds = new DataSet();
   ListBox1.DataSource = ds;
   ListBox1.DataTextField = "countryid";
   ListBox1.DataValueField = "countryname";

Post a Comment