ASP.Net DataList Item FindControl error: Object reference not set to an instance of an object

Last Reply 28 days ago By pandeyism

Posted 28 days ago

I am trying to pass data from datalist to another page Gridview.

here is getting error Object reference not set to an instance of an object.

on this line please guide.

String myquery = "select * from Customer where CustomerID=" + customerID.Text;

here is my html

<fieldset style="width:53%">
    <legend>Repeat Cloumns in Datalist</legend>
    <asp:DataList ID="dlemp" runat="server" CellPadding="5" RepeatColumns="4" Font-Names="Verdana" Font-Size="Small"
           CellSpacing="5" RepeatDirection=" Horizontal">
           <HeaderStyle VerticalAlign="Middle" Font-Size="20PX" BackColor="Black" ForeColor="White" />
      <HeaderTemplate> 
      Customer Details  
      </HeaderTemplate>
    <ItemTemplate>
      <asp:Image  ID="imgEmp" runat="server" Width="100px" Height="80px"  ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' />      
     <b>Customer Name: </b>             
                <asp:Label ID="lblename" runat="server" Text='<%# Eval("CustomerName") %>'></asp:Label> <br />
                <b>Image Name:</b>
                <asp:Label style="text-align:justify" ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label><br />
           <b>Customer ID: </b> <asp:Label ID="lblID" runat="server" Text=' <%# Eval("CustomerID") %>'></asp:Label>
        <asp:CheckBox ID="CheckBox1"  runat="server" Text="Select" />
               </ItemTemplate>
               <FooterTemplate>        
               </FooterTemplate>
    </asp:DataList>
</fieldset>
<asp:Button ID="btn_Add" runat="server" Text="Add To Cart" OnClick="btn_Add_Click" />

C#

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add("CustomerName");
            dt.Columns.Add("Name");
            dt.Columns.Add("CustomerID");
            dt.Columns.Add("Data");
        
            foreach (DataListItem item in dlemp.Items)
            {
                Label customerID = item.FindControl("CustomerID") as Label;
                CheckBox selectedpizza = item.FindControl("CheckBox1") as CheckBox;
                if (selectedpizza.Checked)
                {
                    if (Session["Buyitems"] == null)
                    {
                        dr = dt.NewRow();
                        con.Open();
                        String myquery = "select * from Customer where CustomerID=" + customerID.Text;
                        SqlCommand cmd = new SqlCommand();
                        cmd.CommandText = myquery;
                        cmd.Connection = con;
                        SqlDataAdapter da = new SqlDataAdapter();
                        da.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        dr["CustomerName"] = 1;
                        dr["CustomerID"] = ds.Tables[0].Rows[0]["customerID"].ToString();
                        dr["Name"] = ds.Tables[0].Rows[0]["name"].ToString();
                        dr["Data"] = ds.Tables[0].Rows[0]["size"].ToString();
                        dr["productimage"] = ds.Tables[0].Rows[0]["Data"].ToString();
                    
                        dt.Rows.Add(dr);
                        Session["buyitems"] = dt;
                    }
                }
            }
            Response.Redirect("ShowCart.aspx");
        }

Please Guide

Posted 28 days ago
akhter says:
Label customerID = item.FindControl("CustomerID") as Label;

 Change above line of code using below line. You are finding wrong id.

Label customerID = item.FindControl("lblID") as Label;