locked
DataRowView is null in Repeater Item Data Bound event RRS feed

  • Question

  • User-209021226 posted

    I have a repeater control which is not returning the DataRowView in its RowDataBound event, it returns Null object exception.

    Friday, November 22, 2019 2:56 AM

Answers

  • User-1780421697 posted

    i have tested your code and removed the 

    DataSourceMode="DataReader"

    and it work fine

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 12:20 PM
  • User-1780421697 posted

    You can try the ObjectDataSource with custom object and you will get its object as Item.DataType  in Item Data Bound Event.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 12:21 PM

All replies

  • User-1780421697 posted

    Can you please explain what type of datasource ? do you check the item type in your itemdatabound event handler?

    Friday, November 22, 2019 3:04 AM
  • User288213138 posted

    Hi Mushar,

    Mushar

    I have a repeater control which is not returning the DataRowView in its RowDataBound event, it returns Null object exception.

    According to your description, I can't reproduce your question. can you show me your code?

    I get the DataRowView in the RowDataBound event as below code:

     protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                DataRowView dr = (DataRowView)e.Item.DataItem;
            }

    The result:

    Best regards,

    Sam

    Friday, November 22, 2019 6:57 AM
  • User-209021226 posted
       
    
        <asp:Repeater ID="rptrAds" runat="server" DataSourceID="adsDataSource"  OnItemDataBound="rptr_ItemDataBound">
             <HeaderTemplate>
          <p class="text"><b>Actions:</b> 
          <asp:LinkButton ID="btnAdd" Runat="server"
                          CssClass="text"
                          CommandName="add">Add New Record
          </asp:LinkButton></p>
          <table cellpadding="4" cellspacing="0" width="100%">
          <tr class="tableheading">
             <td width="80%">AdId</td>
             <td width="20%">Title</td>
          </tr>
       </HeaderTemplate>
       <ItemTemplate>
          <tr class="tabletext">
             <td class="tabletext"><%# Eval("AdId") %></td>
    
             <td align="center" class="tabletext">
             <asp:LinkButton ID="btnEdit" Runat="server"
                             CssClass="tabletext" CommandName="edit"
                             CommandArgument='<%# Eval("AdId")
                             %>'>Edit</asp:LinkButton>
             |
             <asp:LinkButton ID="btnDelete" Runat="server"
                             CssClass="tabletext" CommandName="delete"
                             CommandArgument='<%# Eval("AdId")
                             %>'>Delete</asp:LinkButton></td>
          </tr>
       </ItemTemplate>
       <AlternatingItemTemplate>
          <tr class="tabletext_gray">
             <td class="tabletext"><%# Eval("Title") %></td>
    
             <td align="center" class="tabletext">
             <asp:LinkButton ID="btnEdit" Runat="server"
                             CssClass="tabletext" CommandName="edit"
                             CommandArgument='<%# Eval("AdId")
                             %>'>Edit</asp:LinkButton>
             |
             <asp:LinkButton ID="btnDelete" Runat="server"
                             CssClass="tabletext" CommandName="delete"
                             CommandArgument='<%# Eval("AdId")
                             %>'>Delete</asp:LinkButton></td>
          </tr>
       </AlternatingItemTemplate>
       <FooterTemplate>
       </table>
       </FooterTemplate>
        </asp:Repeater>
        <asp:SqlDataSource ID="adsDataSource" runat="server" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT * FROM [Ad]"></asp:SqlDataSource>
    
       

    Monday, November 25, 2019 4:57 AM
  • User-209021226 posted
            protected void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                { 
                    DataRowView drView = e.Item.DataItem as DataRowView;
                    //Other code hided
    
                } 
            }

    Monday, November 25, 2019 5:38 AM
  • User-1780421697 posted

    I think you need to pass datatable as data source not data reader to get a data row in itemdatabound event, like if you have a classtype object binded then you get class type object as item in item data bound event, similarly in this case for datarow there should be datatable. 

    Monday, November 25, 2019 5:45 AM
  • User288213138 posted

    Hi Mushar,

    DataRowView drView = e.Item.DataItem as DataRowView;

    I tested your code, it works fine in my side. please check your code and database.

    Below is my result, I have 5 data in my database.

    Best regards,

    Sam

    Monday, November 25, 2019 10:23 AM
  • User-1780421697 posted

    i have tested your code and removed the 

    DataSourceMode="DataReader"

    and it work fine

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 12:20 PM
  • User-1780421697 posted

    You can try the ObjectDataSource with custom object and you will get its object as Item.DataType  in Item Data Bound Event.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 12:21 PM