public CheckBox[] cb = null;
int z=0;
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           int count = (int)countcmd.ExecuteScalar();
           Int32 k = Convert.ToInt32(Session["TZ"]);

            cb = new CheckBox[count - k];

           OleDbCommand cmd = new OleDbCommand("Select   Member_Name from Member_Master", con);
            dr = cmd.ExecuteReader();
            
            
            while (dr.Read())
            {
            
             //creating check box 
              
              
                    cb[z] = new CheckBox();
                    cb[z].Text = dr["Member_Name"].ToString();
                    Panel2.Controls.Add(cb[z]);
                    Panel2.Controls.Add(new LiteralControl("</br>"));
                    // if (z == (t - 1)) { Button6.Visible = true; }
                    z = z + 1;
              
            }
        }
    }
protected void Button6_Click(object sender, EventArgs e)
{
      //proccess to get the value of checked box and insert into database
        cb[tzx] = new CheckBox();
    
        for (int x = 0; x < cb.Length; x++)//ERROR OOCUR HERE
        {
            //cb[x] = new CheckBox();
            if (cb[x].Checked == true)
            {
    
                       //remain code..
              
            }
        }
        
    }

A couple of things. You're only establishing the checkbox array in the Page_Load event and not on a postback. The button click event happens during a postback. Inherently, the array will not exist for the event.

I would suggest using a CheckBoxList on your presentation page instead of your approach of adding checkboxes and literals to a panel control. I would move the initialization of the list to the Page_Init event, as viewstate is managed between Init and Load. In this example, it might not be entirely necessary*, but it's a good habit to develop if you are programmatically adding inputs to a web page.

Adding items to a CheckBoxList is simple, it's the same as adding items to anything like a ListBox or DropDownList.

while (dr.Read())
	{
		// assumes existence of CheckBoxList control on presentage page called chkMembers
              
		ListItem item = new ListItem(dr["Member_Name"].ToString());
		chkMembers.Items.Add(item); 
	}

Working with the checkboxes is similarly easy in your click event

foreach (ListItem item in chkMembers)
	{
		if (item.Selected)
		{
			// process
		}
	}

*Should be unnecessary because like you were trying in the original example, you would only need to add the items to the control once. With your original approach, you would definitely need to move to Page_Init, and generate the array/controls each time the page is loaded, and then let the normal events of the page life cycle handle your pastback data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.