Strange listview error

dakota97

Centurion
Joined
Nov 14, 2003
Messages
116
Location
Pennsylvania
Hi all,

I have a really strange thing happening, and I can't for the life of me figure out why. What I have is a listview control set up to display trouble tickets that are stored in my DB. When a user selects a listview item and clicks the VIEW button, it loads another form, enters the information from the DB into the controls of the form, and then displays the form to the user.

Now for the the problem. Everything works great until it comes to the part of the code to display the next form. I get the following error:


Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: startIndex


Here's my complete code for the view button click event:


Code:
Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click

        If lstTickets.SelectedItems.Count = 0 Then
            MessageBox.Show("You must select a ticket to view")
        Else

            strSelTicket = lstTickets.SelectedItems(lstTickets.SelectedItems(0).Index).Text

            Try
                'Retrieve the information for the selected ticket
                DbConn.Open()
                DbCommand.CommandText = "SELECT * FROM Tickets WHERE TicketNum = '" & strSelTicket & "'"
                DbCommand.Connection = DbConn
                DbReader = DbCommand.ExecuteReader

                'Populate the text boxes for the next form and display it to the user
                If DbReader.Read Then
                    strTicketNum = DbReader("TicketNum")
                    strTicketType = DbReader("Type")
                    Dim frmViewTicket As New ViewTicket
                    frmViewTicket.Text = "Details for ticket #" & DbReader("TicketNum")
                    frmViewTicket.txtName.Text = Convert.ToString(DbReader("Name"))
                    frmViewTicket.txtCoName.Text = Convert.ToString(DbReader("CoName"))
                    frmViewTicket.txtAdd1.Text = Convert.ToString(DbReader("Add1"))
                    frmViewTicket.txtAdd2.Text = Convert.ToString(DbReader("Add2"))
                    frmViewTicket.txtCity.Text = Convert.ToString(DbReader("City"))
                    frmViewTicket.txtState.Text = Convert.ToString(DbReader("State"))
                    frmViewTicket.txtZip.Text = Convert.ToString(DbReader("Zip"))
                    frmViewTicket.txtPhone1.Text = Convert.ToString(DbReader("Phone1"))
                    frmViewTicket.txtPhone2.Text = Convert.ToString(DbReader("Phone2"))
                    frmViewTicket.txtPhone1Ext.Text = Convert.ToString(DbReader("Phone1Ext"))
                    frmViewTicket.txtPhone2Ext.Text = Convert.ToString(DbReader("Phone2Ext"))
                    frmViewTicket.txtMobile.Text = Convert.ToString(DbReader("Mobile"))
                    frmViewTicket.txtFax.Text = Convert.ToString(DbReader("Fax"))
                    frmViewTicket.txtEmail1.Text = Convert.ToString(DbReader("Email1"))
                    frmViewTicket.txtEmail2.Text = Convert.ToString(DbReader("Email2"))
                    frmViewTicket.txtContact1.Text = Convert.ToString(DbReader("Contact1"))
                    frmViewTicket.txtContact2.Text = Convert.ToString(DbReader("Contact1"))
                    frmViewTicket.txtDepartment.Text = Convert.ToString(DbReader("Department"))
                    frmViewTicket.txtModel.Text = Convert.ToString(DbReader("Model"))
                    frmViewTicket.txtSerial1.Text = Convert.ToString(DbReader("Serial1"))
                    frmViewTicket.txtSerial2.Text = Convert.ToString(DbReader("Serial2"))
                    frmViewTicket.txtDescription.Text = Convert.ToString(DbReader("Description"))
                    frmViewTicket.txtCategory.Text = Convert.ToString(DbReader("Category"))
                    frmViewTicket.txtResolution.Text = Convert.ToString(DbReader("Resolution"))
                    frmViewTicket.txtEnteredBy.Text = Convert.ToString(DbReader("EnteredBy"))
                    frmViewTicket.txtTimestamp.Text = Convert.ToString(DbReader("Entered"))

                    'Set the appropriate status for the requested ticket
                    If DbReader("Status") = "Open" Then
                        frmViewTicket.txtStatus.Text = "Pending"
                    Else
                        frmViewTicket.txtStatus.Text = Convert.ToString(DbReader("Status"))
                    End If

                    frmViewTicket.Show()
                    DbConn.Close()
                    Me.Close()

                Else
                    MessageBox.Show("There was a problem reading the information from the database.  Please contact technical support.")
                End If
            Catch ex As Exception
                MessageBox.Show("An error has occured.  Please contact technical support with the following message:" & ControlChars.NewLine & ControlChars.NewLine & ex.Message)
                DbConn.Close()
            End Try
        End If

    End Sub

The part the I don't get is that the only thing that I am doing with an index is getting the ticket number of the selected item so that I can insert it into my query statement. I even tried taking out the reference to the index of the selected item, and still nothing. Any ideas?

Thanks in advance,

Chris
 
try changing:

If lstTickets.SelectedItems.Count = 0 Then
MessageBox.Show("You must select a ticket to view")
Else

strSelTicket = lstTickets.SelectedItems(lstTickets.SelectedItems(0).Index).Text

to:

If lstTickets.SelectedIndex = -1 Then
MessageBox.Show("You must select a ticket to view")
Else
strSelTicket = lstTickets.Items(lstTickets.SelectedIndex).Text


and see if that helps.
 
Didn't work. Selectedindex isn't a property of the listview control. It's with the listbox control. I tried changing to a listbox, and rearranging a few things but I need to use the listview control instead.

Thanks,

Chris
 
Last edited:
Try this

If lstTickets.SelectedItems.Count < 0 Then
MessageBox.Show("You must select a ticket to view")
Else

strSelTicket = lstTickets.Items.Item(lstTickets.items.indexof(lsttickets.selecteditem))

This will work for the listbox control
 
Thanks for your help all, but I figured it out. It wasn't the listview control that was giving me the problem. The problem was when the second form loaded, I was doing some string manipulation, and the index error was from that. I didn't put any code in to handle emtpy strings, and when I tried to insert a character into the string, it couldn't find the index point to insert it. If that makes any sense, then you see my problem....lol

Thanks again,

Chris
 
Back
Top