Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

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)

0
John Gates, CISSP
Asked:
John Gates, CISSP
  • 13
  • 6
  • 3
  • +1
1 Solution
 
Mikal613Commented:
at the end of the function

li = nothing
Columns = nothing
i = nothing
0
 
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Same error occurs even with that signified.
0
 
Mikal613Commented:
did you step through the code? Which line does it crash on?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

  Me.list.ListItems.Clear()
0
 
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?  
0
 
Mikal613Commented:
try Me.list.Clear()
0
 
John Gates, CISSPSecurity ProfessionalAuthor Commented:
'Clear' is not a member of 'AxMSComctlLib.AxListView'.
0
 
Mikal613Commented:
why dont you use a regular listview from the toolbox?
0
 
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?
0
 
Mikal613Commented:
not sure
0
 
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.
0
 
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
0
 
John Gates, CISSPSecurity ProfessionalAuthor Commented:
An Error Occurred: 91 Object reference not set to an instance of an object.
Interop.MSComctlLib
System.NullReferenceException: Object
0
 
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
0
 
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Same error results.
0
 
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
     
0
 
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...
0
 
reshamCommented:
Did you debug though it second time? What was the values of me.list and Me.list.ListItems?
0
 
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...
0
 
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?
0
 
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
0
 
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-
0
 
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
0
 
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
0
 
John Gates, CISSPSecurity ProfessionalAuthor Commented:
Thanks that one does it.  .NET is a strange animal...  Oh well it's a process LOL Thanks again!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 13
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now