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

Last Reply 11 months ago By pandeyism

Posted 11 months 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" />
      Customer Details  
      <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" />
<asp:Button ID="btn_Add" runat="server" Text="Add To Cart" OnClick="btn_Add_Click" />


        protected void btn_Add_Click(object sender, EventArgs e)
            DataTable dt = new DataTable();
            DataRow dr;
            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();
                        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();
                        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();
                        Session["buyitems"] = dt;

Please Guide

Posted 11 months 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;