Populate List Data

METHOD 1: Without any datasource control
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New AccessDataSource
ds = AccessDataSource1
Response.Write(ds.ConnectionString)
Dim mycon As New OleDbConnection
Dim conStr As String = ds.ConnectionString
mycon.ConnectionString = conStr
mycon.Open()
Response.Write("

" & mycon.State & "

")
Dim myCmd As New OleDbCommand
myCmd.Connection = mycon
myCmd.CommandType = Data.CommandType.Text
myCmd.CommandText = ds.SelectCommand.ToString
Try
Dim dr As OleDbDataReader = myCmd.ExecuteReader
Response.Write(dr.HasRows)
'Dim str As String = ""
While dr.Read
ListBox1.Items.Add(dr.Item("CustomerID") & _
"," & dr.Item("CompanyName") & "," & _
dr.Item("LastName") & "," & _
dr.Item("City") & "," _
& dr.Item("HomePhone"))
'Response.Write(str)
End While
dr.Close()
Catch ex As System.Data.OleDb.OleDbException
Response.Write(ex.Message)
End Try
mycon.Close()
End Sub
End Class

METHOD 2: Without any datasource control

Imports System.Data.OleDb
Partial Class CodeBound
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New AccessDataSource
ds = AccessDataSource1
Dim sqlstr As String = "Select * from Customers"
ds.SelectCommand = sqlstr
ListBox1.DataSource = ds
ListBox1.DataValueField = "CustomerID"
ListBox1.DataTextField = "CompanyName"
ListBox1.DataBind()
End Sub
End Class



Post a Comment

Previous Post Next Post