ListView Exception

A

Aaron

This will probably be the first of many postings for me on this
subject since to matter what I do with a listbox it throws multiple
exceptions, even after using sample code from the help and other
archive postings...

I have a listview on my form and I'm created three columns. I'm using
InTheHand ADOCE but that has no bearing on why I'm getting exceptions,
it just happens to be how I'm pulling my data. The following code
throws many exceptions, but the only one I've seemed to have gotten
lately is

System.ArgumentOutOfRangeException on this line:

clmTicket.SubItems.Add(ticketStop)

This doesn't make any sense cause I have three columns defined, so how
can adding a value to the second column be out of range??? Below is my
full code from my form's Activated event:

'create the first column with a ListViewItem object
Dim clmTicket As New ListViewItem
Dim ticketStop As String
Dim ticketStreet As String

'lstServices.HeaderStyle = ColumnHeaderStyle.Nonclickable
lstServices.Items.Clear()

'Get the route information
rsTickets.Open("SELECT * FROM tblServiceTickets WHERE RouteID
= " & G_ROUTE_ID, cn)
If Not rsTickets.EOF And Not rsTickets.BOF Then
rsTickets.MoveFirst()
While Not rsTickets.EOF
ticketStop = CStr(rsTickets.Fields("Stop").Value())
ticketStreet = rsTickets.Fields("Street").Value()

clmTicket.Text =
CStr(rsTickets.Fields("Number").Value())
clmTicket.SubItems.Add(ticketStop)
clmTicket.SubItems.Add(ticketStreet)

'finally, add the complete item to the listview
lstServices.Items.Add(clmTicket)

rsTickets.MoveNext() 'get new values from next record
End While
End If
rsTickets.Close()

Thanks
 
A

Alex Feinman [MVP]

You are forgetting to do clmTicket = new ListViewItem inside the loop
This is a bane of VB developers. Dim X as New Y(). It is so easy to forget
that a) constructors, espcecially for forms are often large pices of code
that take time to execute and b) that they are executed just once
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ListView Selected Item 9

Top