user823911
user823911

Reputation: 277

Populate Listbox with Access Database?

I'm unable to get the listbox display values from my database.This is the code. ANy thoughts on how to crack it?

Imports System.Data.OleDb
Public Class Form1
    Dim dbConnection As OleDbConnection
    Dim dbCommand As OleDbCommand
    Dim strInsert As String
    Dim dbDataAdapter As OleDbDataAdapter
    Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =atg.mdb"
    Dim dtATG As DataTable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dtTest As New DataTable
        dtTest.Columns.Add("Col1", GetType(Integer))


        For i As Integer = 1 To 10
            dtTest.Rows.Add(i, "Row " & i.ToString)
        Next

        ListBox1.DisplayMember = "Col1"
        ListBox1.ValueMember = "Col1"
        ListBox1.DataSource = dtTest.DefaultView
    AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    MessageBox.Show(ListBox1.SelectedValue.ToString)

End Sub

End Class

Upvotes: 0

Views: 11686

Answers (2)

Wizzo
Wizzo

Reputation: 1

Use the System.IO.File.ReadAllLines:

ListBox1.Items.AddRange(System.IO.File.ReadAllLines("C:\folder\Your File.txt"))

Upvotes: 0

Jude Cooray
Jude Cooray

Reputation: 19872

Here is a modified version of your code that works.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dtTest As New DataTable
        dtTest.Columns.Add("Col1", GetType(Integer))


        For i As Integer = 1 To 10
            dtTest.Rows.Add(i)
        Next

        ListBox1.DisplayMember = "Col1"
        ListBox1.ValueMember = "Col1"

        ListBox1.DataSource = dtTest
End Sub

Upvotes: 1

Related Questions