none
Databinding to ListBox w/attributes problem RRS feed

  • Question

  • So, my education has hit another hopefully small hiccup...

    I've got two listboxes, the destination box is initially empty but I need to be able to transfer items into it (and potentially back to the source listbox in event of an erroneous transfer).  The initial databinding was pretty much effortless but now that I'm trying to enable the transfer it's become a trainwreck.  After reading and getting a suggestion from Telerik this is what I have so far...

     <telerik:RadListBox ID="ticketListBox" runat="server" AllowTransfer="True" 
                  DataKeyField="ID" DataSourceID="EntityDataSource1" 
                  TransferToID="splitListBox" CssClass="style1" Width="330px" 
                  AutoPostBackOnTransfer="True" EnableViewState="True"
                  
                  ontransferred="ticketListBox_Transferred" AllowTransferDuplicates="True" 
                  onitemdatabound="ticketListBox_ItemDataBound" SelectionMode="Multiple" 
                  DataTextField="Name" CausesValidation="False" 
                 >
    <ButtonSettings TransferButtons="All"></ButtonSettings>
               <ItemTemplate>
           <table style="font-family: Arial, Helvetica, sans-serif;  font-size: medium; color: #C36029">
           <td>
            <%# DataBinder.Eval(Container, "Attributes['Quantity']")%>       
           </td>
           <td>
           <%# DataBinder.Eval(Container, "Attributes['Name']")%>
           </td>
            <td>
           <%# DataBinder.Eval(Container, "Attributes['Special']")%>
           </td>
            <td>
            <%# DataBinder.Eval(Container, "Attributes['Price']")%>
          
           </td>    
           </table>               
           </ItemTemplate>       
              </telerik:RadListBox>
        
              <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
                  ConnectionString="name=BlahBlahEntities" 
                  DefaultContainerName="BlahBlahEntities" EnableFlattening="False" 
                  EntitySetName="Ticket_Details" 
                  
                  Select="it.[Name], it.[Price], it.[Special], it.[Ticket_], it.[ID], it.[Quantity]" 
                  Where="it.[Ticket_]==@Ticket">
                  <WhereParameters>
                      <asp:QueryStringParameter DbType="Int16" Name="Ticket" QueryStringField="Tkt" />
                  </WhereParameters>
              </asp:EntityDataSource>

    I'm leaving out the second listbox for the sake of brevity... The section of code behind that (so far is throwing a fit is:

      protected void ticketListBox_ItemDataBound(object sender, RadListBoxItemEventArgs e)
            {
    
                e.Item.Attributes.Add("Quantity", (e.Item.DataItem as DataRowView)["Quantity"].ToString());
                e.Item.Attributes.Add("Ticket#", (e.Item.DataItem as DataRowView)["Ticket#"].ToString());
                e.Item.Attributes.Add("ID", (e.Item.DataItem as DataRowView)["ID"].ToString());
                e.Item.Attributes.Add("Sent", (e.Item.DataItem as DataRowView)["Sent"].ToString());
                e.Item.Attributes.Add("Special", (e.Item.DataItem as DataRowView)["Special"].ToString());
                e.Item.Attributes.Add("Name", (e.Item.DataItem as DataRowView)["Name"].ToString());
                e.Item.Attributes.Add("Price", (e.Item.DataItem as DataRowView)["Price"].ToString());
                e.Item.DataBind();
            
            }

    I'm getting a null reference exception as soon as the databound event fires (on the first line). I can see the DataItem is returning a full set of data so I assume it's got something to do with how I'm trying to assign the attributes but............... I'm lost.

    As always, thanks in advance for any suggestions!

    Friday, September 21, 2012 1:32 PM

Answers

  • Hi PaulBinCT,

    Welcome to MSDN Forum.

    This is caused by the conversion from e.Item.DataItem to DataRowView failed. Please add a breakpoint at this line to see what type the e.Item.DataItem actually is.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by PaulBinCT Monday, September 24, 2012 1:37 PM
    Monday, September 24, 2012 2:41 AM
    Moderator

All replies

  • Hi PaulBinCT,

    Welcome to MSDN Forum.

    This is caused by the conversion from e.Item.DataItem to DataRowView failed. Please add a breakpoint at this line to see what type the e.Item.DataItem actually is.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by PaulBinCT Monday, September 24, 2012 1:37 PM
    Monday, September 24, 2012 2:41 AM
    Moderator
  • Thanks Allen!  What I wound up doing to keep this moving along was (out of desperation) replacing the entity datasource with a simple SQL query and it worked like a charm.  Have to chalk it up to learning EF I guess, but next time around I'll have a better idea how to handle it.

    Thanks again!

    Monday, September 24, 2012 1:39 PM