Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > multiselect listbox producing duplicate index error
 

multiselect listbox producing duplicate index error


multiselect listbox producing duplicate index error

I did search before asking this question.

I filled an AuthorsListbox with a list of Authors. I set multiple selection to simple, as an article can have more than one author. The article has already been saved and has a unique ID. Each author has a unique ID.

When I select more than one author I get a duplicate index error.

If I change mutlipleselection to one, and save the authors one at a time, the database is updatged properly

Here's the code:


    Private Sub btnSaveAuthors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAuthors.Click
        Try
            'Dim idx As Integer
            Dim i As Integer
            Dim iT As Integer
            Dim i1 As Integer = Me.frmTopicID
            Dim tta As New TopicAuthorAssignmentsTableAdapter
            For i = 0 To lstAuthors.SelectedIndices.Count - 1
                Me.lstAuthors.SelectedIndex = lstAuthors.SelectedIndices(i)
                iT = CInt(Me.lstAuthors.SelectedValue)
                'MsgBox(i1.ToString & s & iT.ToString)
                tta.InsertQuery(i1, iT)
            Next
        Catch ex As Exception
            MessageBox.Show("Type = " & ex.GetType.ToString & vbCr & "Message = " & ex.Message & vbCr & "Source: " & ex.Source & vbCr & "Stack Trace: " & ex.StackTrace)
        End Try
    End Sub
   
Can anybody tell me what I'm doing wrong?

dennist685   

dennist685  Saturday, October 22, 2005 11:44 AM

The big thing to remember is that listboxes SelectedItems are cast as System.Object because each of the types of datasources you can bind to them have different forms, so you must cast them (using CType()) to the correct form.

Dim oRow as System.Data.Common.DbDataRecord

For i = 0 To lstAuthors.SelectedItems.Count - 1
   ' Get the DbDataRecord of the selected item
   oRow = CType(lstAuthors.SelectedItems(i) , System.Data.Common.DbDataRecord)
   ' Get the contents of the field that holds the selected value
   iT = CInt(oRow(lstAuthors.ValueMember))
   tta.InsertQuery(i1, iT)

Next

Rick Hodder  Wednesday, October 26, 2005 5:03 AM
The problem is that when you set the SelectedIndex property, you are changing what is selected - from many (SelectedIndices) to one (SelectedIndex).

Lets say that you have the 2nd and 4th items selected. On the first pass through the loop you set the SelectedIndex to SelectedIndices(0) which is 2. Doing this causes the listbox to have only one thing selected (2). On the second pass through the loop you try to access SelectedIndices(1) which no longer exists

You would do better to loop through the SelectedItems collection. That wont change the selected items
Rick Hodder  Tuesday, October 25, 2005 3:36 AM
I tried this code and got the same error

For i = 0 To lstAuthors.SelectedItems.Count - 1

Me.lstAuthors.SelectedItem = lstAuthors.SelectedItems

iT = CInt(Me.lstAuthors.SelectedValue)

tta.InsertQuery(i1, iT)

Next

dennist685

dennist685  Tuesday, October 25, 2005 9:02 AM
Hi Dennis,

The code above has the same problem as the SelectedIndices example you posted earlier. By setting SelectedItem, you are changing the contents of the SelectedItems collection to 1 item.

What you are trying to do is read what is selected, and that takes a little extra work in a multi-select situation. 

When a listbox is bound to a dataset or datatable, SelectedItem is a DataRowView, which has a Row[<fieldname>] collection property.

The code below assumes that you have set the ValueMember of lstAuthors.

Dim oRow as DataRowView

For i = 0 To lstAuthors.SelectedItems.Count - 1
   ' Get the DataRowView of the selected item
   oRow = CType(lstAuthors.SelectedItems(i) ,DataRowView)
   ' Get the contents of the field that holds the selected value
   iT =
CInt(oRow.Row(lstAuthors.ValueMember))
   tta.InsertQuery(i1, iT)

Next

Hope this helped
Rick

Rick Hodder  Tuesday, October 25, 2005 3:27 PM

Thanks for the help, but your code came up with a glitch.  I think we're getting closer.

Type = System.InvalidCastException
Message = Unable to cast object of type'System.Data.Common.DbDataRecord to type 'System.Data.DataRowView'.
...
line 488, which is
 oRow = CType(lstAuthors.SelectedItems(i), DataRowView)
 
I can't say I understand the need for a datarowview, or exactly what it does, or the syntax in the statement above.  But as soon as it works, I guess I have some reading to do.

dennist685

dennist685  Wednesday, October 26, 2005 4:40 AM

The big thing to remember is that listboxes SelectedItems are cast as System.Object because each of the types of datasources you can bind to them have different forms, so you must cast them (using CType()) to the correct form.

Dim oRow as System.Data.Common.DbDataRecord

For i = 0 To lstAuthors.SelectedItems.Count - 1
   ' Get the DbDataRecord of the selected item
   oRow = CType(lstAuthors.SelectedItems(i) , System.Data.Common.DbDataRecord)
   ' Get the contents of the field that holds the selected value
   iT = CInt(oRow(lstAuthors.ValueMember))
   tta.InsertQuery(i1, iT)

Next

Rick Hodder  Wednesday, October 26, 2005 5:03 AM
Nicely done, Rick.  Thank you very much.  Now I've got some reading to do.

dennist685
dennist685  Wednesday, October 26, 2005 9:53 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridViewComboBoxColumn FormatException
• DataGrid currency problem?
• Override how GridView gets data from DataRow
• Problem with SQLDataAdapter update method with DataTable
• how can i know which field only has changed?
• DataColumnChanging problems...
• BindingSource.ResetBindings()
• Editing in Gridview control in .net 2.0
• multiuser program by using access 2000 database in the Visual Studio 20005
• Cannot enlarge the width of last column of DataGridView?