Forum Moderators: open

Message Too Old, No Replies

Data Transfer between two ASP.NET ListBox Controls

Receive error when clicking add or remove button

         

bradford7

4:44 pm on Mar 3, 2003 (gmt 0)

10+ Year Member



I have been attempting to use two listboxes with an add and a remove button in between these listboxes.

I am trying to pull data from sql which populates the first listbox with a list of all pages on the website.

The second box is where we would like to add or remove the pages that users can edit. This will be populated from a table. But the goal is to have the ability to select multiple items in the first box and click the add button to move those items to the second list box. Also I would like to select items from the second listbox and click the remove button and this removes the items from this box.

The code below is what I am using which comes from: [123aspx.com ] The error message is below as well.

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

Dim strSelection As String

If Not allRoles.SelectedItem Is "" Then

strSelection = allRoles.SelectedItem.Text

If allRoles.Items.FindByText(strSelection) Is "" Then
EditableRoles.Items.Add(strSelection)
EditableRoles.SelectedIndex = 0
allRoles.Items.Remove(strSelection)
End If
End If

End Sub

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
Dim strSelection As String

If Not EditableRoles.SelectedItem Is "" Then

strSelection = EditableRoles.SelectedItem.Text

If EditableRoles.Items.FindByText(strSelection) Is "" Then
allRoles.Items.Add(strSelection)
allRoles.SelectedIndex = 0
EditableRoles.Items.Remove(strSelection)
End If
End If
End Sub

Error Message:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 231: If Not allRoles.SelectedItem Is "" Then
Line 232:
Line 233: strSelection = allRoles.SelectedItem.Text
Line 234:
Line 235: If allRoles.Items.FindByText(strSelection) Is "" Then

Thank you for your help.

hakre

5:21 pm on Mar 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it's only a guess, but maybe the use of allRoles.SelectedItem instead of allRoles.SelectedItem.Text in line 233 will prevent you getting this error.

bradford7

5:36 pm on Mar 3, 2003 (gmt 0)

10+ Year Member



Hakre, thank you for your help. With this change, I am unable to build the project without error. I still receive the same error.

Thanks again. I am still working on it over here.