Cannot access a disposed object named "TreeView".

I am getting the error when I try to close the form.  Cannot access a disposed object named "TreeView".

Not sure what why.

Thanks
Private Sub frmMHContactLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connection1.Open()
        Me.Size = New Size((Me.MdiParent.Size.Width - 17), Me.MdiParent.Size.Height)

        Dim daSections As New SqlDataAdapter("SELECT DISTINCT TOP 100 PERCENT convert(varchar(10),dbo.tblMHCIntake.dtmIntake,101) as intakedate,dtmintake, DATENAME(dw, dbo.tblMHCIntake.dtmIntake) AS dayname FROM         dbo.tblMHCIntake   where dtmintake > '01/01/2008'ORDER BY dbo.tblMHCIntake.dtmIntake DESC", Connection1)
        Dim daContent As New SqlDataAdapter("SELECT DISTINCT  TOP 100 PERCENT dbo.tblPatient.strLName + ', ' + dbo.tblPatient.strFName AS name, convert(varchar(10),dbo.tblMHCIntake.dtmIntake,101) as intakedate,dbo.tblMHCIntake.dtmIntake, dbo.tblMHCIntake.strStaff, convert(varchar(10),dbo.tblPatient.dtmDOB,101) as dtmdob, dbo.tblPatient.strPatientId, dbo.tblMHCIntakeDetail.lngIntakeId FROM       dbo.tblMHCIntake INNER JOIN dbo.tblMHCIntakeDetail ON dbo.tblMHCIntake.lngIntakeId = dbo.tblMHCIntakeDetail.lngIntakeId right JOIN dbo.tblPatient ON dbo.tblMHCIntake.lngPatientId = dbo.tblPatient.lngPatientId where dtmintake > '1/1/2008'ORDER BY dbo.tblMHCIntake.dtmIntake DESC", Connection1)

        daSections.Fill(objDS, "dtSections")
        daContent.Fill(objDS, "dtContent")

        objDS.Relations.Add("SectionToContent", _
        objDS.Tables("dtSections").Columns("dtmIntake"), _
        objDS.Tables("dtContent").Columns("dtmIntake"))



        For Each rowSupp In objDS.Tables("dtSections").Rows

            nodeSupp = New TreeNode
            nodeSupp.Text = rowSupp("dtmIntake")

            TreeView1.Nodes.Add(nodeSupp)
            For Each rowProd In rowSupp.GetChildRows("SectionToContent")
                nodeProd = New TreeNode
                nodeProd.Text = rowProd("intakedate") & " - " & rowProd("strStaff") & " - " & rowProd("name") & " - " & rowProd("dtmdob") & " - " & rowProd("strPatientid")
                'need to databind rowProd ("lngIntakeId")
                nodeProd.Tag = rowProd("lngIntakeID")
                nodeSupp.Nodes.Add(nodeProd)

            Next

        Next
        'clean up

      

    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        MHIntakeID = e.Node.Tag

        'subclose()
        If MHIntakeID <> "" Then
       
            Dim child As New frmMHClientContact
            child.MdiParent = Me.MdiParent

            child.Show()


            Me.Close()

        End If
    End Sub

Open in new window

running32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Coralie CrumrineCommented:
What is the clean up? Are you calling any other code here that might be disposing of the TreeView? Is there any code in the form_closing or form_closed events?
0
running32Author Commented:
No close events.  I commented out the client up so I am disposing of any thing.  Thanks
0
Mohamed AbowardaSoftware EngineerCommented:
This exception occur because the program is trying to access TreeView1 after the form has been disposed.

Try this:
Private Sub frmMHContactLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connection1.Open()
        Me.Size = New Size((Me.MdiParent.Size.Width - 17), Me.MdiParent.Size.Height)

        Dim daSections As New SqlDataAdapter("SELECT DISTINCT TOP 100 PERCENT convert(varchar(10),dbo.tblMHCIntake.dtmIntake,101) as intakedate,dtmintake, DATENAME(dw, dbo.tblMHCIntake.dtmIntake) AS dayname FROM         dbo.tblMHCIntake   where dtmintake > '01/01/2008'ORDER BY dbo.tblMHCIntake.dtmIntake DESC", Connection1)
        Dim daContent As New SqlDataAdapter("SELECT DISTINCT  TOP 100 PERCENT dbo.tblPatient.strLName + ', ' + dbo.tblPatient.strFName AS name, convert(varchar(10),dbo.tblMHCIntake.dtmIntake,101) as intakedate,dbo.tblMHCIntake.dtmIntake, dbo.tblMHCIntake.strStaff, convert(varchar(10),dbo.tblPatient.dtmDOB,101) as dtmdob, dbo.tblPatient.strPatientId, dbo.tblMHCIntakeDetail.lngIntakeId FROM       dbo.tblMHCIntake INNER JOIN dbo.tblMHCIntakeDetail ON dbo.tblMHCIntake.lngIntakeId = dbo.tblMHCIntakeDetail.lngIntakeId right JOIN dbo.tblPatient ON dbo.tblMHCIntake.lngPatientId = dbo.tblPatient.lngPatientId where dtmintake > '1/1/2008'ORDER BY dbo.tblMHCIntake.dtmIntake DESC", Connection1)

        daSections.Fill(objDS, "dtSections")
        daContent.Fill(objDS, "dtContent")

        objDS.Relations.Add("SectionToContent", _
        objDS.Tables("dtSections").Columns("dtmIntake"), _
        objDS.Tables("dtContent").Columns("dtmIntake"))

        For Each rowSupp In objDS.Tables("dtSections").Rows

            nodeSupp = New TreeNode
            nodeSupp.Text = rowSupp("dtmIntake")

            TreeView1.Nodes.Add(nodeSupp)
            For Each rowProd In rowSupp.GetChildRows("SectionToContent")
                nodeProd = New TreeNode
                nodeProd.Text = rowProd("intakedate") & " - " & rowProd("strStaff") & " - " & rowProd("name") & " - " & rowProd("dtmdob") & " - " & rowProd("strPatientid")
                'need to databind rowProd ("lngIntakeId") 
                nodeProd.Tag = rowProd("lngIntakeID")
                nodeSupp.Nodes.Add(nodeProd)
            Next

        Next
        'clean up

    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        If Me.IsDisposed = False Then
            MHIntakeID = e.Node.Tag

            'subclose() 
            If MHIntakeID <> "" Then
                Dim child As New frmMHClientContact
                child.MdiParent = Me.MdiParent
                child.Show()
                Me.Close()
            End If
        End If
    End Sub

Open in new window

0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

running32Author Commented:
still got the same error.
0
CodeCruiserCommented:
Set a break point in the AfterSelect event and see if its being called during the close event.

Also add a condition like you have for Me.IsDisposed

If TreeView1.IsDisposed = False Then
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
running32Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.