Getting an error from the following code

On initial invocation the code works as expected once the operation is tried again (To refresh) I get a error:

Code Start --->

Private Sub LoadData(ByRef nSizingType As ImageSizingTypes)

        'Dim list As Object
        Dim li As Object
        Dim Columns As Object
        Dim i As Object

        Try

            If rt.RecordCount = 0 Then
            Else

                Me.list.ListItems.Clear()


                rt.Requery()
                With rt
                    .MoveFirst()
                    While Not .EOF

                        'Me.list.Visible = False
                        Me.list.Checkboxes = False
                        Me.list.FullRowSelect = True

                        li = Me.list.ListItems.Add(, , rt.Fields(0))

                        Columns = 6

                        For i = 1 To Columns - 1


                            si = li.ListSubItems.Add(, , rt.Fields(3))   'Last
                            si1 = li.ListSubItems.Add(, , rt.Fields(2))   'First
                            si2 = li.ListSubItems.Add(, , rt.Fields(4))   'Department

                            If IsDBNull(rt.Fields(4).Value) Then
                                si3 = li.ListSubItems.Add(, , "")
                            Else

                                si3 = li.ListSubItems.Add(, , rt.Fields(1))   'Requestor
                            End If

                            If IsDBNull(rt.Fields(70).Value) = True Then

                                si5 = li.ListSubItems.Add(, , "")
                            Else

                                si5 = li.ListSubItems.Add(, , rt.Fields(70))   'Username
                            End If

                            If IsDBNull(rt.Fields(71).Value) = True Then

                                si6 = li.ListSubItems.Add(, , "")
                            Else

                                si6 = li.ListSubItems.Add(, , rt.Fields(71))   'Password
                            End If

                            si4 = li.ListSubItems.Add(, , rt.Fields(0))   ' ID (THIS MUST BE THE LAST LINE!!)

                            ' System.Windows.Forms.Application.DoEvents()

                        Next
                        .MoveNext()

                        'System.Windows.Forms.Application.DoEvents()

                    End While
                End With

            End If
            '/* Now that the control contains data, this
            '/* causes the columns to resize to fit the items
            Call listAutosizeControl((EditScreen.DefInstance.list))

        Catch E As Exception
            ' Let the user know what went wrong.
            MsgBox("An Error Occurred: " & Err.Number() & Err.Description())
            ' If (x800A003A) Then
            '    MsgBox("Already exists!")
            '   End If
        End Try

    End Sub

<-- Code end

Error -->

An Error Occurred:
99 External component has thrown an exception.
Interop.MSComctlLib
System.Runtime.InteropServices.SEHException: External component has thrown an exception.
   at MSComctlLib.ListItemsClass.Clear()
   at StaffAdditionProject.EditScreen.LoadData(ImageSizingTypes& nSizingType)

LVL 18
John Gates, CISSPSecurity ProfessionalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Mikal613Commented:
at the end of the function

li = nothing
Columns = nothing
i = nothing
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Same error occurs even with that signified.
Mikal613Commented:
did you step through the code? Which line does it crash on?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

John Gates, CISSPSecurity ProfessionalAuthor Commented:
The error clearly shows that the code errors out here:

  Me.list.ListItems.Clear()
John Gates, CISSPSecurity ProfessionalAuthor Commented:
To give more information my code is taking items from a database and putting them in the list box.  When the procedure is called again the list needs to be cleared first or the data will simply append to the listview.  Is there a more .net way I should be doing this?  
Mikal613Commented:
try Me.list.Clear()
John Gates, CISSPSecurity ProfessionalAuthor Commented:
'Clear' is not a member of 'AxMSComctlLib.AxListView'.
Mikal613Commented:
why dont you use a regular listview from the toolbox?
John Gates, CISSPSecurity ProfessionalAuthor Commented:
This is a converted project from vb6.  will the same code work against that listview?  Can I make that control have ledger colors?
Mikal613Commented:
not sure
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Ok...  Then I need to get the above code working then.  I saw no easy way to accomplish this with the .NET listview.
appariCommented:

try changing
Me.list.ListItems.Clear()

to

for iCnt  as integer =  Me.list.ListItems.Count -1 to 0 step -1
    Me.list.ListItems.remove(iCnt)
next
John Gates, CISSPSecurity ProfessionalAuthor Commented:
An Error Occurred: 91 Object reference not set to an instance of an object.
Interop.MSComctlLib
System.NullReferenceException: Object
Mikal613Commented:
acc to that your object is gone

make these module level public variables and then test again

Dim li As Object
        Dim Columns As Object
        Dim i As Object
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Same error results.
reshamCommented:
An Error Occurred: 91 Object reference not set to an instance of an object.
Interop.MSComctlLib
System.NullReferenceException: Object

This occurs when object is not instanticated. Have you tried to QuickWatch while debugging step by step. My guess is Me.list.ListItems has value Nothing.

Try this way
If Not Me.list.ListItems Is Nothing then
     Me.list.ListItems.Clear()
Else
    'Create New ListItemCollection
    'Add items to collection
    ' assign this collection to Me.list.ListItems
    For e.g check this code out. It is from MSDN so not about your control but similar to what I wanted to say
    ' Create a new ListItemCollection.
                    Dim listBoxData As New ListItemCollection()
                    ' Add items to the collection.
                    listBoxData.Add(New ListItem("apples"))
                    listBoxData.Add(New ListItem("bananas"))
                    listBoxData.Add(New ListItem("cherries"))
                    listBoxData.Add("grapes")
                    listBoxData.Add("mangos")
                    listBoxData.Add("oranges")
                    ' Set the ListItemCollection as the data source for ListBox1.
                    ListBox1.DataSource = listBoxData

Endif

Hope this helps
     
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Same error occurs...  The first time this code runs when the box is fresh runs fine.  It is after data is in it and it is tried again that it fails...
reshamCommented:
Did you debug though it second time? What was the values of me.list and Me.list.ListItems?
John Gates, CISSPSecurity ProfessionalAuthor Commented:
When it gets to item 55 the listbox disappears and that error occurs...  Not too sure where to go from here...
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Did you debug though it second time? What was the values of me.list and Me.list.ListItems?

How do I get these values at error?
reshamCommented:
I mean did you put breakpoint on that line and check the value of me.list and Me.list.ListItems object through QuickWatch?
Do not run that line. Just check values
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Here is what I do see

When I put a breakpoint in here:

----->  Me.list.ListItems.Remove(iCnt)

It cycles through the entries deleting them until it gets down to 50 then for some reason it is setting the list visibility to false...  Mind you I do not have code in hiding the visibility...  This is where the problem is.  What could possibly be making the list property change to visible = false during code execution?


-D-
appariCommented:

i checked it here, the problem is you have to clear subitems also otherwise its giving error at somepoint.

try this

change
for iCnt  as integer =  Me.list.ListItems.Count -1 to 0 step -1
    Me.list.ListItems.remove(iCnt)
next

to

                For iCnt = Me.list.ListItems.Count To iMin Step -1
                    list.ListItems(iCnt).ListSubItems.Clear()
                    list.ListItems.Remove(iCnt)
                Next
appariCommented:
sorry try this one

For iCnt = Me.list.ListItems.Count To 1 Step -1
                    list.ListItems(iCnt).ListSubItems.Clear()
                    list.ListItems.Remove(iCnt)
                Next

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
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Thanks that one does it.  .NET is a strange animal...  Oh well it's a process LOL Thanks again!
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.