Click here to Skip to main content
15,918,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing an application that has a database which is accdb, and it has three tables in it. I tried to populate the the tables into treeview in vb.net but it is giving me an error. Please I need help.

Its pops up error at this point, its telling me An unhandled exception of type 'System.InvalidOperationException' occured in System,.Data.dll

Additional information: There is already an open Datareader associated with this Command Which most be Closed First

Dim querry2 As String = "select ID,Heading,Level from tblgrouplevel"
       cmd2 = New OleDbCommand(querry2, con)
       dat2 = cmd.ExecuteReader
       While dat2.Read()
           list2(dat2.Item(1).ToString).Add(dat2.Item(2).ToString)
       End While


What I have tried:

VB.NET
Imports System.Data.OleDb
Public Class Form1
    Private con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
                               "Data Source=Makebills.accdb;")
    Dim list1 As New List(Of String)
    Dim list2 As New Dictionary(Of String, List(Of String))
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        con.Open()
        Dim cmd As New OleDbCommand
        Dim cmd2 As New OleDbCommand
        Dim dat As OleDbDataReader
        Dim dat2 As OleDbDataReader

        Dim querry As String = "select ID,Levels from tbltoplevels"
        cmd = New OleDbCommand(querry, con)
        dat = cmd.ExecuteReader
        While dat.Read()
            list1.Add(dat.Item(1).ToString)
            list2.Add(dat.Item(1).ToString, New List(Of String))
        End While


        Dim querry2 As String = "select ID,Heading,Level from tblgrouplevel"
        cmd2 = New OleDbCommand(querry2, con)
        dat2 = cmd.ExecuteReader
        While dat2.Read()
            list2(dat2.Item(1).ToString).Add(dat2.Item(2).ToString)
        End While

        con.Close()

        For Each item In list1
            Dim GrandfatherNOde As TreeNode = TreeView1.Nodes.Add(item)
            For Each Str As String In list2(item)
                Dim fatherNode As TreeNode = GrandfatherNOde.Nodes.Add(Str)
            Next
        Next

    End Sub
End Class
Posted
Updated 9-Sep-20 22:42pm
v3
Comments
Richard Deeming 9-Sep-20 11:36am    
If you want us to help you fix an error, you need to tell us what the error is and where it occurs.

Click the green "Improve question" link and add the full details of the error to your question. Remember to tell us which line of code the error is thrown from.
Bj Molo 10-Sep-20 7:18am    
Thank you for the Quick respond to my issue i do really appreciate, its still giving me error
Dim settings As ConnectionStringSettings = ConfigurationManager.ConnectionString("connectionString")
Return New OleDbConnection(settings.ConnectionString)

Thank you
Bj Molo 11-Sep-20 19:45pm    
Thank You I was able resolve it...

1 solution

You're reusing the same connection across multiple queries. Don't do that. Instead, create the connection when it's required, and dispose of it as soon as you've finished with it. Wrap all disposable objects in a Using block to make sure they're always disposed of properly.

You should also move your connection string to your application's configuration file, so that you don't have to recompile your application when you deploy it.
VB.NET
Imports System.Configuration
Imports System.Data.OleDb

Public Class Form1
    Private Shared Function CreateConnection() As OleDbConnection
        Dim settings As ConnectionStringSettings = ConfigurationManager.ConnectionString("connectionString")
        Return New OleDbConnection(settings.ConnectionString)
    End Function
    
    Private Shared Sub LoadTopLevels(ByVal list1 As List(Of String), ByVal list2 As Dictionary(Of String, List(Of String)))
        Using con As OleDbConnection = CreateConnection()
            Using cmd As New OleDbCommand("select ID,Levels from tbltoplevels", con)
                con.Open()
                Using reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                    While reader.Read()
                        Dim key As String = reader.Item(1).ToString()
                        list1.Add(key)
                        list2.Add(key, New List(Of String))
                    End While
                End Using
            End Using
        End Using
    End Sub
    
    Private Shared Sub LoadGroupLevels(ByVal list2 As Dictionary(Of String, List(Of String)))
        Using con As OleDbConnection = CreateConnection()
            Using cmd As New OleDbCommand("select ID,Heading,Level from tblgrouplevel", con)
                con.Open()
                Using reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                    While reader.Read()
                        Dim key As String = reader.Item(1).ToString()
                        list2(key).Add(reader.Item(2).ToString())
                    End While
                End Using
            End Using
        End Using
    End Sub
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim list1 As New List(Of String)
        Dim list2 As New Dictionary(Of String, List(Of String))
        LoadTopLevels(list1, list2)
        LoadGroupLevels(list2)
        
        For Each item In list1
            Dim GrandfatherNOde As TreeNode = TreeView1.Nodes.Add(item)
            For Each Str As String In list2(item)
                Dim fatherNode As TreeNode = GrandfatherNOde.Nodes.Add(Str)
            Next
        Next
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900