This is a migrated thread and some comments may be shown as answers.
Can't move last item in RadListBox
1 Answer 94 Views
This is a migrated thread and some comments may be shown as answers.
DGraham
Top achievements
Rank 1
DGraham asked on 28 Aug 2009, 03:48 PM
Hi,

The items transfer fine between the two list boxes unless its the last item in the list box, where I get the following error:

Error occurs on first line of RadListBoxContacts_Transferred (VB code)
e.SourceListBox.SelectedItem.DataKey

Error:
'Object reference not set to an instance of an object.'

 please see code below:

ASPX
<telerik:RadListBox ID="RadListBoxContacts" runat="server" Skin="Telerik" AutoPostBack="true" 
                                                                AutoPostBackOnTransfer="true" DataKeyField="contactID" DataTextField="Description" 
                                                                Width="240px" Height="200px" AllowTransfer="true" TransferToID="RadListBoxServiceContacts" 
                                                                ButtonSettings-ShowTransferAll="false" EnableViewState ="true">  
                                                                <ButtonSettings ShowTransferAll="False" /> 
                                                            </telerik:RadListBox> 
 
 
<telerik:RadListBox ID="RadListBoxServiceContacts" runat="server" Skin="Telerik" 
                                                                DataKeyField="contactID" DataTextField="Description" Width="245px" Height="200px" 
                                                                AutoPostBack="True" EnableViewState ="true" > 
                                                            </telerik:RadListBox> 

VB
Private Sub RadListBoxServiceContacts_Deleted(ByVal sender As ObjectByVal e As Telerik.Web.UI.RadListBoxEventArgs) Handles RadListBoxServiceContacts.Deleted  
        Session("Avalability") = "Delete" 
        Me.lblServiceContactDescription.Text = "Select a contact to view details" 
        Me.lblServiceContactDetails.Text = "" 
    End Sub 
    Private Sub RadListBoxServiceContacts_Inserted(ByVal sender As ObjectByVal e As Telerik.Web.UI.RadListBoxEventArgs) Handles RadListBoxServiceContacts.Inserted  
        Session("Avalability") = "Insert" 
        Me.lblContactDescription.Text = "Select a contact to view details" 
        Me.lblContactDetails.Text = "" 
    End Sub 
 
    Private Sub RadListBoxContacts_Transferred(ByVal sender As ObjectByVal e As Telerik.Web.UI.RadListBoxTransferredEventArgs) Handles RadListBoxContacts.Transferred  
 
        Dim contactID As String = CStr(e.SourceListBox.SelectedItem.DataKey)  
        Dim serviceID As String = CStr(Me.RadListBoxServices.SelectedItem.DataKey)  
 
        If CStr(Session("Avalability")) = "Delete" Then 
            objWebService.removeContactFromService(contactID, serviceID)  
        ElseIf CStr(Session("Avalability")) = "Insert" Then 
            objWebService.addContactToService(contactID, serviceID)  
        End If 
        Me.RadListBoxContacts.Items.Clear()  
        Me.RadListBoxServiceContacts.Items.Clear()  
 
        Me.RadListBoxServiceContacts.DataSource = objWebService.getServicesContacts(CInt(Me.RadListBoxServices.DataKeys.Item(Me.RadListBoxServices.SelectedIndex)))  
        Me.RadListBoxServiceContacts.DataBind()  
 
        Me.RadListBoxContacts.DataSource = objWebService.getEmployeeContactsNotInService(CInt(Me.RadListBoxServices.DataKeys.Item(Me.RadListBoxServices.SelectedIndex)), Me.RadComboBoxSelectedEmployee.SelectedValue, "")  
        Me.RadListBoxContacts.DataBind()  
 
    End Sub 

The last four lines are executed in the Page_Load to initially populate the list boxes

As can be seen above the list boxes are re-bound to the database via a web service every time an item is inserted or deleted.

Any help or pointes in the right direction would be great.

Many Thanks.

1 Answer, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 04 Sep 2009, 08:51 AM
Hello DGraham,

The Transferred event fires when an item is already transferred. This means that the item is removed from the source and is added to the destination. Then the next item in the source is selected so that you can proceed with the transfer. However, when the last item is being transferred, there are no more items to select in the source hence the null reference error. I suggest you to use the e.Items collection to obtain the items being transferred.

Example:

Sub RadListBoxContacts_Transferred(sender As Object, e As RadListBoxTransferredEventArgs) 
    Dim item As RadListBoxItem = e.Items(0) 
End Sub 
 
 

I hope this helps.

Kind regards,
Genady Sergeev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Asked by
DGraham
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
Share this question
or