0

I have a listbox in VB.NET that allows users to select multiple categories. I need to put these categories into a database and need the index value from the listbox of the categories as the database works off IDs. SelectedItems (with an s) does not work in the net application.

I have tried the following code:

For Each category As ListItem In CategoryListBox.Items
    If category.Selected Then
        Dim courseCategory As New CourseCategory
        courseCategory.CourseID = course.ID
        courseCategory.CategoryID = CategoryListBox.SelectedIndex
        Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
            courseCategoryEntities.CourseCategories.Add(courseCategory)
            courseCategoryEntities.SaveChanges()
        End Using
    End If
Next

When iterating through the loop the code that is:

courseCategory.CategoryID = CategoryListBox.SelectedIndex

works correctly the first time around.

On the second iteration of the loop, it goes to the next selected item however returns the index for the first selected value. How do I return the values of the other indexes selected?

7
  • try SelectedIndices for a collection if them Commented Jul 28, 2014 at 19:15
  • Any of the multiple commands (SelectedItems, SelectedIndices) do not show up when coding. I believe they would work with the desktop application. I have not found a way to call them in my web application. Commented Jul 28, 2014 at 19:16
  • you might want to (re)tag your question accordingly to attract the right help Commented Jul 28, 2014 at 19:17
  • I thought I have tagged it vb.net is this not the tag I should be using? Commented Jul 28, 2014 at 19:19
  • winforms, wpf, webforms, asp, asp.NET etc - theres nothing in the question to indicate this is a web app Commented Jul 28, 2014 at 19:20

2 Answers 2

1

It depends on what ID you need to pass to your database. If it is truly the index of the ListItem in the ListBox, then you would use:

courseCategory.CategoryID = CategoryListBox.Items.IndexOf(category); 

That may not be the best approach however. Maybe the order of the ListItems changes, messing up your indexes. You probably want to store the actual CategoryID on each ListItem. The Value property works well for that. You set the column you want as the DataValueField like so:

<asp:ListBox ID="CategoryListBox" runat="server"
    DataValueField="CategoryID "></asp:ListBox>

So as you are looping through each ListItem in the ListBox and checking if it is selected, just use it's Value property.

For Each category As ListItem In CategoryListBox.Items
    If category.Selected Then
        Dim courseCategory As New CourseCategory
        courseCategory.CourseID = course.ID
        courseCategory.CategoryID = category.Value;
        Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
            courseCategoryEntities.CourseCategories.Add(courseCategory)
            courseCategoryEntities.SaveChanges()
        End Using
    End If
Next
Sign up to request clarification or add additional context in comments.

2 Comments

This seems like a much more elegant solution. If the indices ever get changed though, current data in the database becomes corrupted.
Your first bit of code was exactly what I was looking for I could not figure out how to call the index of the category variable to save me. I understand the dangers of changing the indexes, but as long as the categories are maintained database side the index of the listitem +1 is the index of the category in the table.
0

Fixed the code by deselecting the item in the listbox after it was successfully saved.

   End Using
category.Selected = False
End If

This solved my problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.