Search code examples
c#asp.netlistviewitem

ListViewDataItem does not contain a property with the name 'CommentId' when using DataBinder.Eval


I have a ListView which displays all the comments made by users on a specific news article.

I need the comment's and author's ids for each comment so I can show a delete button next to it (if it is made by the respective user).

The problem is that the line DataBinder.Eval(dataItem, "CommentId") throws the following error

System.Web.UI.WebControls.ListViewDataItem' does not contain a property with the name 'CommentId'.

I also tried changing from CommentId to Comments.CommentId, but with no luck.

What I am doing wrong? Any help is appreciated.

Thanks!

In News.aspx

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="ListView1_ItemDataBound">

                    <ItemTemplate>
                        <span style="">
                            <table>
                                <tr>
                                    <td style="width=60px">
                                        <asp:Label Text='<%# Eval("UserName") + ":"%>' runat="server" ID="UserNameLabel" Font-Bold="true" /><br />
                                    </td>
                                    <td>
                                        <asp:Label Text='<%# Eval("body") %>' runat="server" ID="bodyLabel" /><br />

                                    </td>
                                    <td>
                                        <asp:Button ID="DeleteCommentButton" runat="server" Text="Delete" OnClick="DeleteCommentButton_Click" />
                                    </td>
                                </tr>
                            </table>
                            <br />
                        </span>
                    </ItemTemplate>

                    <LayoutTemplate>
                        <div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
                        <div style="">
                        </div>
                    </LayoutTemplate>

                </asp:ListView>
                <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:DefaultConnection %>' SelectCommand="SELECT Comments.CommentId, Users.UserId, Comments.body, Users.UserName FROM Comments INNER JOIN Users ON Comments.UserId = Users.UserId WHERE NewsId = @NewsId">
                    <SelectParameters>
                        <asp:Parameter Name="NewsId" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>

In News.aspx.cs:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;

            int commentId = (int)DataBinder.Eval(dataItem, "CommentId");
...

Solution

  • I solved it like this:

    ListViewDataItem dataItem = e.Item as ListViewDataItem; 
    int commentId = (int)DataBinder.Eval(dataItem.DataItem, "CommentId");
    Guid authorId = (Guid)DataBinder.Eval(dataItem.DataItem, "UserId");