Object reference not set to an instance of an object.-i got null value of checkbox ar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satiss7pwr
    New Member
    • Jan 2010
    • 41

    #1

    Object reference not set to an instance of an object.-i got null value of checkbox ar

    Code:
    public CheckBox[] cb = null;
    int z=0;
    protected void Page_Load(object sender, EventArgs e)
    {
    	if (!IsPostBack)
            {
    		cb = new CheckBox[count - k];
    		//Database code.........
    		while (dr.Read())//read data from access database
                	{
    			cb[z] = new CheckBox();
    	                cb[z].Text = dr["Member_Name"].ToString();
                            Panel2.Controls.Add(cb[z]);
                            Panel2.Controls.Add(new LiteralControl("</br>"));
                            z = z + 1;
    		}
    	}
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
            for (int x = 0; x < cb.Length; x++)//ERROR IS HERE:Object reference not set to an instance of an object.
            {
    		//processing check boxes	
    	}
    }
    Last edited by tlhintoq; Jan 19 '10, 05:58 AM. Reason: [CODE] ... your code here ... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You made an array for checkboxes, but you did not initialize each element of that array. You need to make a checkbox for each element and assign it to the element.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Better yet, use a CheckBoxList control.

      All you'll have to do is retrieve the data from your database, specify that the data table returned from this is to be used as the DataSource for the CheckBoxList control. Then specify what field should be used as the "value" (using the CheckBoxList.Da taValueField property ) and what field should be used for the "text" (using the CheckBoxList.Da taTextField property)....and then call the CheckBoxList.Da taBind method to bind the data source to the CheckBoxList.

      -Frinny

      Comment

      Working...