Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Object reference not set to an instance of an object.

protected void cmdSave_Click(object sender, EventArgs e)
    {
        string strNames = string.Empty;
        CheckBoxList Chkboxx = (CheckBoxList)PlaceHolder1.FindControl("Chkbox");
        foreach (ListItem em in Chkboxx.Items)  //-------- (Showing error)
        {
            if (em.Selected)
            {
                strNames += em.Value + ", ";
            }
        }

        string final_name = strNames.Substring(0, strNames.Length - 2);
        lblNames.Text = final_name;

    }


Actually I am adding Checkbox control dynamically :

C#
protected void ddl_varient_SelectedIndexChanged1(object sender, EventArgs e)
   {
       string query = "select prd_vrtyvalue_id,varient_value from tbl_ProductVariety_Value  where varient='" + ddl_varient.SelectedItem.Text + "' " +
                         " order by varient_value asc ";

    DataTable abc = new DataTable();
    SqlDataAdapter ada = new SqlDataAdapter(query, new CommonClass().connection());
       ada.Fill(abc);

       ChkboxList.ID = "Chkbox";
       for (int i = 0; i < abc.Rows.Count; i++)
       {
           ChkboxList.Items.Add(new ListItem(abc.Rows[i]["varient_value"].ToString(), abc.Rows[i]["prd_vrtyvalue_id"].ToString()));
       }
       ChkboxList.RepeatColumns = 2;
       PlaceHolder1.Controls.Add(ChkboxList);
   }


Can Anybody tell me what exactly i am doing wrong !
Posted
Updated 18-Mar-13 0:15am
v2
Comments
Nitin S 18-Mar-13 6:09am    
PlaceHolder1.FindControl("Chkbox") is returning null value, check whether that id exists in the placeholder
Chinmaya C 18-Mar-13 6:33am    
yes.. I am also having same view as Nitin. :)
Need to check whether "Chkbox" control exists in the panel or not.
Mas11 18-Mar-13 6:15am    
Please look on updated code !
Nitin S 18-Mar-13 6:41am    
You wont be able to get the chkbox after clicking the save button as the viewstate of ChkboxList is no maintained by asp.net.
You have created an in-memory object of the checkboxlist class.
Mas11 18-Mar-13 6:44am    
Ya exactly Nitin ! Sometimes I makes a silly mistakes. Anyway thanks a lot guys for your support :)

Hey you need to loop through the place holder and check what items are inside, asp will create a different id that you expect
foreach(Control ctl in PlaceHolder1.Controls) { 
                                if(ctl is CheckBoxList) { 
                                        CheckBoxListlst cbl = (CheckBoxList)ctl; 
                                                                       } 
                        }


Hope this helps
 
Share this answer
 
v4
Comments
Mas11 18-Mar-13 6:34am    
Thanks for the answer! Now I know that after running your code that there was no control added in placeholder. But see in my code I was added it in the code : "PlaceHolder1.Controls.Add(ChkboxList)"
frostcox 18-Mar-13 6:41am    
You need to re-create your control on every postback. Dynamic controls added to the page during a previous postback go to the garbage collector.
Mas11 18-Mar-13 6:44am    
5* for it!
frostcox 18-Mar-13 6:50am    
Glad I could help.
Hi,

You have add dynamic control at page load event not any selected change event...
so that when you call cmdSave_Click() event the previous created dynamic control are erased....

so you have redraw these control(dynamic) at page load event...then you can assign any value on it..

If you find this helpful, plz vote & Mark as Solution.
Thanks
Asp.Net C# Help Code[^]
Hemant Singh
 
Share this answer
 
v2
Comments
Mas11 18-Mar-13 6:39am    
Ohh sh*t ! Thanks a lot I got it..
Mas11 18-Mar-13 6:44am    
5* for it !

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