Problem in loading data from access to ListView

Nader

Well-known member
Joined
Oct 9, 2008
Messages
55
Programming Experience
Beginner
I tried this code to load the data in ListView1
The problem : it show me this Error: insexout of range exception was unhandeled at this line:
"adi.SubItems.Add(dr.Item("b").ToString)"
VB.NET:
ListView1.Items.Clear()
        Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" 
                & "Data Source =" & Application.StartupPath & "\Data.mdb ")
        Dim cmd As New OleDbCommand("SELECT a, sum(b) FROM Table3 Group by a ", conn)

        conn.Open()

        Dim dr As OleDbDataReader = cmd.ExecuteReader
        Do While dr.Read()
            Dim adi As New ListViewItem(dr.Item("a").ToString)
            adi.SubItems.Add(dr.Item("b").ToString) ' [COLOR="SeaGreen"]the error is here[/COLOR]       
    ListView1.Items.Add(adi)
        Loop
        conn.Close()
        ListView1.View = View.Details[/HIGHLIGHT]
I treid this code without that line like this and it succeed with me
VB.NET:
Dim adi As New ListViewItem(dr.Item("a").ToString)
      ListView1.Items.Add(adi)
 
It seems to be that you would want to declare this object once before the loop instead of creating it each time through.

Dim adi As
 
Back
Top