Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to bind multiple database fields to ListBox control in ASP.NET

Author: Nikki Smith
Download Source Code :

In this article, I will show you how you can bind multiple database fields to ListBox control in ASP.NET


Sometimes you need to bind two or more than two database fields to a ListBox control. You can combine two or more fields in your query to get data from database to make it one field and then you can bind this single field to ListBox.


  1. Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site and a ListBox control in the Web Form

    <asp:ListBox ID="ListBox1" runat="server" Height="500px" Width="300px"></asp:ListBox>
  3. Namespaces used in the code

    Visual Basic
    Imports System.Data
    Imports System.Data.SqlClient
    Visual C#
    using System.Data;
    using System.Data.SqlClient;
  4. Write below code in Page Load method to get data from database and to bind it to ListBox

    In below code, I have written a query to get data from Customers table of NORTHWIND database. You can see I have combined two fields (ContactName and ContactTitle) to make it one field (ContactPerson). I have used dash sign (-) three times in between two fields to separate both. ListBox control is bound to DataSet object and I have provided DataTextField as ContactPerson and DataValueField as CustomerID.    
    Visual Basic
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim text As String = "SELECT CustomerID, (ContactName + '---' + ContactTitle) AS ContactPerson FROM Customers"
        Dim connString As String = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True"
        Dim conn As New SqlConnection(connString)
        Dim cmd As New SqlCommand(text, conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        ListBox1.DataSource = ds
        ListBox1.DataTextField = "ContactPerson"
        ListBox1.DataValueField = "CustomerID"
    End Sub
    Visual C#
    protected void Page_Load(object sender, EventArgs e)
        string text = "SELECT CustomerID, (ContactName + '---' + ContactTitle) AS ContactPerson FROM Customers";
        string connString = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(text, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ListBox1.DataSource = ds;
        ListBox1.DataTextField = "ContactPerson";
        ListBox1.DataValueField = "CustomerID";
  5. Now you can see website in your browser 


Add Article Comment:
Name :
Email Address :
Comments :
<< How to get browser information in ASP.NET

Disclaimer - Privacy
© 2002-2017