Hi all,
How to Bind DataTable to ASP. NET ListBox control ?
Hi all,
How to Bind DataTable to ASP. NET ListBox control ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Aug 16, 2011, 11:07 AM
Please look at the code below,Here you can bind only one column value in DataTextField property on listbox to display that column values only.
Default.aspx code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
Default.aspx.vb code
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con.Open()
str = "select * from employee"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "employee")
ListBox1.DataValueField = "empid"
ListBox1.DataTextField = "empname"
ListBox1.DataSource = ds
ListBox1.DataMember = "employee"
ListBox1.DataBind()
con.Close()
End Sub
End Class
Thanks
If this post helps you mark it as answer