Click here to Skip to main content
15,884,966 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help!!

This is the .aspx code
C#
<%@ Page Title="" Language="C#" MasterPageFile="~/shopmaster.master" AutoEventWireup="true" CodeFile="Productdetails.aspx.cs" Inherits="Productdetails" %>
<%@ MasterType VirtualPath="~/shopmaster.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
    
     

    
    

    
    <asp:DataList ID="DataList1" runat="server" DataKeyField="pcode" 
        DataSourceID="SqlDataSource1">
        <ItemTemplate>
        <div style="width:100%">
            <table style="width: 45%; float:left">
                <tr>
                    <td>
                        <asp:Image ID="Image1" runat="server" Height="100%" 
                            ImageUrl='<%# Eval("image")%>' Width="90%" />
                    </td>
                </tr>
            </table>
            <table style="width: 50%; float:right">
                <tr>
                    <td>
                        <span style="color: #FF0000">Product Neme</span>:<asp:Label ID="Label1" runat="server" Text='<%# Eval("pname")%>'></asp:Label>
                        <br /><br />
                        <span style="color: #FF0000">Product Code</span>:<asp:Label ID="Label2" runat="server" Text='<%# Eval("pcode")%>'></asp:Label>
                        <br /><br />
                        <span style="color: #FF0000">Product Description</span>:<asp:Label ID="Label3" runat="server" Text='<%# Eval("description")%>'></asp:Label>
                        <br /><br /><br />
                        <span style="color: #FF0000">Price</span>:Rs.<asp:Label ID="Label4" runat="server" Text='<%# Eval("price")%>'></asp:Label>
                        <br /><br /><br />
                        <asp:ImageButton ID="ImageButton1" runat="server" Height="39px" 
                            ImageUrl="~/web/images/add-to-cart-button-blue.png" 
                            onclick="ImageButton1_Click1" 
                            PostBackUrl='<%# Eval("pcode","cart.aspx?pcode={0}") %>' Width="147px" />
                        <br />
                    </td>
                </tr>
            </table>
            </div>
            <br />
            <br />
            <br />
            <br />
            <br />
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [pname], [pcode], [image], [description], [price] FROM [products] WHERE ([pcode] = @pcode)">
        <SelectParameters>
            <asp:QueryStringParameter Name="pcode" QueryStringField="pcode" 
                Type="Decimal" />
        </SelectParameters>
    </asp:SqlDataSource>

    
    


    
</asp:Content>


this is the .aspx.cs code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Data;

public partial class Productdetails : System.Web.UI.Page
{
    string un;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Master.FindControl("Label_UserName").Visible == false)
        {
            DataList1.FindControl("ImageButton1").Visible = false;
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }


    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {

    }
}
Posted
Updated 5-May-14 8:24am
v2
Comments
[no name] 5-May-14 14:33pm    
If we were to assume to that the title of your posting, "Why I am getting the error: "Object reference not set to an instance of an object"" were an actual question then your answer is, "Because you are trying to use the methods or properties of an object that has not been instantiated."
Paulo Augusto Kunzel 5-May-14 14:35pm    
hhahahahaha... You stole my line
[no name] 5-May-14 14:39pm    
My apologies. I will try and type slower next time :-)
Maciej Los 5-May-14 16:00pm    
V e r y s l o w ;)
Paulo Augusto Kunzel 5-May-14 14:36pm    
Hello,
When you debug, what when does it happen?

1 solution

The problem is that DataList1.FindControl("ImageButton1") is null. The reason for that is ImageButton1 is part of a template, so not really ever will be a ImageButton1 control at runtime. The controls you declared in the template used to create - repeatedly - runtime controls according the data the container bind to...
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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