Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to get the value from my labels in a datalist to print out to another label, and i am getting error "Object reference not set to the instance of an object"

Here's my datalist:
XML
<table>
<tr>
<td>
    <asp:DataList ID="ProdList" runat="server" DataKeyField="PRDProdID"
        DataSourceID="SqlDataSource1" RepeatColumns="3" BackColor="LightYellow"
        BorderColor="Black" BorderStyle="Dotted" BorderWidth="2px" CellPadding="10"
        CellSpacing="10" Font-Bold="True" Font-Names="Calibri" Font-Size="Small"
        RepeatDirection="Horizontal" Width="1000px">
        <ItemTemplate>
        <table>
        <tr>
        <td valign="top">
            <asp:Image ID="Image1" runat="server" Height="100" Width="100" />
        </td>
        <td valign="top">
        <table>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDProdIDLabel" runat="server" Text='<%# Eval("PRDProdID") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDCATIDLabel" runat="server" Text='<%# Eval("PRDCATID") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        SKU#:&nbsp;<asp:Label ID="PRDProdCodeLabel" runat="server" Text='<%# Eval("PRDProdCode") %>' />
        </td>
        </tr>
        <tr>
        <td>
        Product:&nbsp;<asp:Label ID="PRDNameLabel" runat="server" Text='<%# Eval("PRDName") %>' />
        </td>
        </tr>
        <tr>
        <td>
        Description:&nbsp;<asp:Label ID="PRDDescriptionLabel" runat="server" Text='<%# Eval("PRDDescription") %>' />
        </td>
        </tr>
        <tr>
        <td>
        Customer Rating:&nbsp;<asp:Label ID="PRDCustomerRatingLabel" runat="server" Text='<%# Eval("PRDCustomerRating") %>' />
        </td>
        </tr>
        <tr>
        <td>
        List Price:&nbsp;<asp:Label ID="PRDListPriceLabel" runat="server" Text='<%# Eval("PRDListPrice", "{0:C}") %>' />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDPictureLabel" runat="server" Text='<%# Eval("PRDPicture") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        In Stock:&nbsp;<asp:Label ID="PRDInStockLabel" runat="server" Text='<%# Eval("PRDInStock") %>' />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDThumbLabel" runat="server" Text='<%# Eval("PRDThumb") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDPictureEnlargeLabel" runat="server" Text='<%# Eval("PRDPictureEnlarge") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        Price:&nbsp;<asp:Label ID="PRDPriceLabel" runat="server" Text='<%# Eval("PRDPrice", "{0:C}") %>' />
        </td>
        </tr>
        </table>
        </tr>
        </td>
        <tr>
        <td>
            <asp:Button ID="addtocartbutton" runat="server" Text="Add to Cart"
                Font-Bold="True" Font-Names="calibri" Font-Size="Smaller"
                CommandName="addtocart" />
        </td>
        </tr>
        </table>
        </ItemTemplate>
    </asp:DataList>
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="White"></asp:Label>



and my codebehind:

VB
Protected Sub ProdList_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles ProdList.ItemCommand
        If e.CommandName = "addtocart" Then

            Dim prodid = DirectCast(e.Item.FindControl("PRDProdID"), Label)
            Dim prodname = DirectCast(e.Item.FindControl("PRDName"), Label)

            Label1.Text = prodname.Text
        Else

        End If
    End Sub


can I not use button command name in itemcommand?
Posted
Updated 30-May-17 2:09am

1 solution

Check this solution:
http://forums.asp.net/t/1204372.aspx/1[^]

Essentially you want to be calling FindControl on your datalist and not on your event argument.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900