Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting this error whenever i am pressing the addcart button.

products.aspx

ASP
    <asp:SqlDataSource ID="SqlDataSource1? runat="server" ConnectionString=" ConnectionStrings:prdcts_accsrsConnectionString1" SelectCommand="SELECT [ProductID], [Name], [Description], [Price], [ImagePath] FROM [Products]">

</div>
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="4"
    RepeatDirection="Horizontal">
    <itemstyle cssclass="box" />
   <itemtemplate>
     <div style="height:320px; width:220px; background:white; padding:30px 0 0 30px">
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>' Visible="false" >
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("ImagePath" , "~/products/Accessories/Images\\{0}") %>' PostBackUrl='<%# Eval("ProductID", "ProductDetails.aspx?ProductID={0}") %>' Height="170px" Width="180px" /><br /></td></tr>
     <asp:Label ID="ImageUrlLabel" runat="server" Text='<%# Eval("ImagePath",  "~/products/Accessories/Images\\{0}") %>' Visible="False"><br />
     <tr> <td class="td">    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'><br /></td></tr>
      <tr> <td class="td">   <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price", "{0:##0.00}") %>'><br /></td></tr>
        <tr><td style="padding:15px 0 0 0">
 <div style="float:left">
     <asp:Button ID="Button1" runat="server" Text="Add To Cart" CssClass="bttn"
          onclick="Button1_Click"/> </div>
 <div style="float:right; padding-removed30px; margin-removed-10px">   <asp:ImageButton ID="ImageButton2" runat="server"  ImageUrl="~/products/TransparentHeart.png" Height="50px" Width="50px"/></div></td></tr>
</table>
</div>
    </itemtemplate>
<br />
<asp:HyperLink ID="CartLink" runat="server" NavigateUrl="~/UserCart.aspx">View Shopping Cart<br />
 <br />
<br />


products.aspx.cs

C#
public partial class Products_Accessories_Products : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text);
        string ProductName = ((Label)DataList1.Controls[0].FindControl("NameLabel")).Text;
        string ProductImageUrl = ((Label)DataList1.Controls[0].FindControl("ImageUrlLabel")).Text;
        int ProductID = int.Parse(((Label)DataList1.Controls[0].FindControl("ProductID")).Text);
        if (Profile.SCart == null)
        {
            Profile.SCart = new ShoppingCartExample.Cart();
        }
        Profile.SCart.Insert(ProductID, Price, 1, ProductName, ProductImageUrl);
        Server.Transfer("~/products/Accessories/Products.aspx");
    }
}



What Should I do? I am getting this error in this line:
C#
int ProductID = int.Parse(((Label)DataList1.Controls[0].FindControl("ProductID")).Text);
Posted
Updated 25-Jun-14 0:46am
v2

Please refer my recent answer : Object reference not set to an instance of an object.[^]
 
Share this answer
 
1.This error means that you are trying to access an null object.

2.In your case the error is be generated by the fact that FindControl("ProductID") could return null, and then you are trying to access the Text property of the null object.
 
Share this answer
 
change
C#
int ProductID = int.Parse(((Label)DataList1.Controls[0].FindControl("ProductID")).Text);

to
C#
int ProductID = int.Parse(((Label)DataList1.Items[0].FindControl("ProductID")).Text);
 
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