locked
Object reference not set to an instance of an object in repeater ? RRS feed

  • Question

  • User-1512811070 posted

    Here I have a table i.e tblp which consists three column i.e id, name, status.Here i am trying to make name stored in column name visible and invisible in page load on the basis of letter stored in column status .To show all the name i have used label i.e Label4 inside the Repeater1.

    Now the condition is that if a id have the data in column name and letter in column status is "s" than that name should be visible in Label4 text,or if letter in column status is "h" then that name should not be invisible in Label4 text,or if letter in column status is "u" then that name should be visible in Label4 text.

    My taget is to show all the name which have letter "s" or "u" in column status and hide all those name which have letter "h" in column status when page load.

    Preblem :-  Now the problem is that if i run it shows error "Object reference not set to an instance of an object" in code behind in line marked as **

    Html used

    <asp:Repeater ID="Repeater1" runat="server" >
                <ItemTemplate>
                    <asp:Label ID="lblid" runat="server" Text='<%#Eval("id") %>'></asp:Label>              
                    <asp:Label ID="Label4" runat="server" Text='<%#Eval("name") %>'></asp:Label>                
                </ItemTemplate>
            </asp:Repeater>

    Code behind

     protected void Page_Load(object sender, EventArgs e)
        {
            Label lblid = (Label)Repeater1.FindControl("lblid");
            DataTable dt = j.getentry(Convert.ToInt32(lblid.Text)); ** /*shows error here*/
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["status"].ToString() == "s")
                {
                    DataTable df = j.getall();
                    if (df.Rows.Count > 0)
                    {
                        Repeater1.DataSource = df;
                        Repeater1.DataBind();
                    }
                }
                else if (dt.Rows[0]["status"].ToString() == "h")
                {
                    Label l1 = (Label)Repeater1.FindControl("Label4");
                    Label l2 = (Label)Repeater1.FindControl("Label5");
                    l1.Visible = false;
                    l2.Visible = false;
                }
                else if (dt.Rows[0]["status"].ToString() == "u")
                {
                    DataTable df = j.getall();
                    if (df.Rows.Count > 0)
                    {
                        Repeater1.DataSource = df;
                        Repeater1.DataBind();
                    }
                }
            }
        }

    method used

     public DataTable getentry(int id)
        {
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
            string sql = "select *from tblp where id=@id";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@id", id);       
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public DataTable getall()
        {
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
            string sql = "select * from tblp";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable df = new DataTable();
            da.Fill(df);
            return df;
    
        }

    Saturday, July 2, 2016 4:53 PM

Answers

  • User2103319870 posted

    Label lblid = (Label)Repeater1.FindControl("lblid"); DataTable dt = j.getentry(Convert.ToInt32(lblid.Text)); ** /*shows error here*/

    You are trying to access the control in repeater control before binding it with data , hence you cannot find the control. Additionally you cannot directly access the controls in ItemTemplate. You need to loop through each records and find the controls.

    //Change your datasource here
                    Repeater1.DataSource = this.Get_TotalDetails();
                    Repeater1.DataBind();
                    //Loop through each records
                    foreach (RepeaterItem item in RP.Items)
                    {
                        //Find the label control
                        Label lbl = (Label)item.FindControl("lblid");
                        if (lbl != null)
                        {
                            //Your code here
                        }
    
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 3, 2016 4:18 AM

All replies

  • User475983607 posted

    This is a very common error and it means you are trying to access members of an object when the object is null.  

    The error message shows the line of code where the error occurred.    Run the the project to step through your logic and find why the object is null.  Is there any way you can tell us which line causes the exception?

    Saturday, July 2, 2016 5:05 PM
  • User-1512811070 posted

    Its in code behind ,i have marked line with ** in second line

    Saturday, July 2, 2016 5:57 PM
  • User2103319870 posted

    Label lblid = (Label)Repeater1.FindControl("lblid"); DataTable dt = j.getentry(Convert.ToInt32(lblid.Text)); ** /*shows error here*/

    You are trying to access the control in repeater control before binding it with data , hence you cannot find the control. Additionally you cannot directly access the controls in ItemTemplate. You need to loop through each records and find the controls.

    //Change your datasource here
                    Repeater1.DataSource = this.Get_TotalDetails();
                    Repeater1.DataBind();
                    //Loop through each records
                    foreach (RepeaterItem item in RP.Items)
                    {
                        //Find the label control
                        Label lbl = (Label)item.FindControl("lblid");
                        if (lbl != null)
                        {
                            //Your code here
                        }
    
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 3, 2016 4:18 AM
  • User61956409 posted

    Hi sudip7139,

    Label lblid = (Label)Repeater1.FindControl("lblid"); DataTable dt = j.getentry(Convert.ToInt32(lblid.Text)); ** /*shows error here*/

    If you debug your code, you will find that variable lblid is null (you could not find the control from Repeater1), so when it go the following code snippet, you see the error.

    DataTable dt = j.getentry(Convert.ToInt32(lblid.Text)); 

    As a2h said, you do not bind Repeater1 with the data before you call FindControl() method to find the control from ItemTemplate.

    Best Regards,

    Fei Han

    Tuesday, July 5, 2016 7:30 AM