Link to home
Start Free TrialLog in
Avatar of running32
running32

asked on

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

Avatar of Coralie Crumrine
Coralie Crumrine
Flag of United States of America image

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?
Avatar of running32
running32

ASKER

No close events.  I commented out the client up so I am disposing of any thing.  Thanks
Avatar of Brad Brett
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

still got the same error.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you