Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:34:27+00:00 2026-06-17T06:34:27+00:00

In this code I dynamically create checkboxes which I populate with data from my

  • 0

In this code I dynamically create checkboxes which I populate with data from my database.

My intention is when I press the btnProba button to show the text property only from the selected checkboxes. But it gives me an error in this row saying that index is out of range! I can’t explain why.

lblProba.Text = myche[0];

public partial class FormEGN : System.Web.UI.Page
{
        string mynewstring;
        List<string> myche = new List<string>();
        CheckBoxList mycheckbox = new CheckBoxList();
        protected void Page_Load(object sender, EventArgs e)
        {
            mynewstring = (string)Session["id2"];

           // lblProba.Text = mynewstring;
            if(!IsPostBack)
            {
                ddlNumberTourists.Items.Add("1");
                ddlNumberTourists.Items.Add("2");
                ddlNumberTourists.Items.Add("3");
            }
        }

        protected void ddlNumberTourists_SelectedIndexChanged(object sender, EventArgs e)
        {
            int numTourists = Convert.ToInt32(ddlNumberTourists.SelectedItem.Text);

            for (int i = 0; i < numTourists; i++)
            {
                Label myLabel = new Label();
                myLabel.ID = "lblAccomodation" + (i + 1).ToString();
                myLabel.Text = "Настаняване Турист" + (i + 1).ToString();
                Page.FindControl("form1").Controls.Add(myLabel);
                DropDownList myDropDownList = new DropDownList();
                myDropDownList.ID = "ddlTourist" + i.ToString();
                Page.FindControl("form1").Controls.Add(myDropDownList);
                Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));

                string connectionString = "Server=localhost\\SQLEXPRESS;Database=EXCURSIONSDATABASE;Trusted_Connection=true";
                string query =
          "SELECT Extra_Charge_ID, Excursion_ID, Amout, Extra_Charge_Description FROM EXTRA_CHARGES WHERE Excursion_ID=" + mynewstring;
                SqlConnection conn = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(query, conn);

                try
                {
                    conn.Open();
                    SqlDataReader rd = cmd.ExecuteReader();
                    int s = 0;

                    while (rd.Read())
                    {
                        // CheckBox myCheckbox = new CheckBox();
                        //   myCheckbox.ID = "ckbExtraCharge" + i.ToString() + s.ToString();
                        // myCheckbox.Text = rd["Extra_Charge_Description"].ToString();
                        //  Page.FindControl("form1").Controls.Add(myCheckbox);
                        //  Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));
                        //  s++;

                        mycheckbox.ID = "chkblextracharge" + i.ToString() + s.ToString();
                        mycheckbox.Items.Add(rd["Extra_Charge_Description"].ToString());
                        Page.FindControl("form1").Controls.Add(mycheckbox);

                        if (mycheckbox.Items[s].Selected == true)
                        {  
                            myche.Add(mycheckbox.Items[s].Text);
                        }
                        s++;
                    }
                }
                catch (Exception ex)
                { }
            }
        }    

        protected void btnProba_Click(object sender, EventArgs e)
        {
            lblProba.Text = myche[0];
        }

        protected void btnReserve_Click(object sender, EventArgs e)
        { 
            string num = Request.QueryString["ExcursionID"];
            Response.Redirect(String.Format("ClintsInformation.aspx?Excursiondate_ID={0}",num));
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:34:28+00:00Added an answer on June 17, 2026 at 6:34 am

    The function ddlNumberTourists_SelectedIndexChanged is the one that set the myche when is find some selected, but the data is lost on the second call where is the

     protected void btnProba_Click(object sender, EventArgs e)
            {
                // here is the issue
                lblProba.Text = myche[0];
            }
    

    The two calls are happening on different post back.

    You need to call the ddlNumberTourists_SelectedIndexChanged(object sender, EventArgs e) on button click and not on every change of the dropdownlist, eg I rename it to CheckWhatIsSelected() and here is the code:

    protected void CheckWhatIsSelected()
            {
                int numTourists = Convert.ToInt32(ddlNumberTourists.SelectedItem.Text);
    
                for (int i = 0; i < numTourists; i++)
                {
                    Label myLabel = new Label();
                    myLabel.ID = "lblAccomodation" + (i + 1).ToString();
                    myLabel.Text = "??????????? ??????" + (i + 1).ToString();
                    Page.FindControl("form1").Controls.Add(myLabel);
                    DropDownList myDropDownList = new DropDownList();
                    myDropDownList.ID = "ddlTourist" + i.ToString();
                    Page.FindControl("form1").Controls.Add(myDropDownList);
                    Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));
    
                    string connectionString = "Server=localhost\\SQLEXPRESS;Database=EXCURSIONSDATABASE;Trusted_Connection=true";
                    string query =
              "SELECT Extra_Charge_ID, Excursion_ID, Amout, Extra_Charge_Description FROM EXTRA_CHARGES WHERE Excursion_ID=" + mynewstring;
                    SqlConnection conn = new SqlConnection(connectionString);
                    SqlCommand cmd = new SqlCommand(query, conn);
    
                    try
                    {
                        conn.Open();
                        SqlDataReader rd = cmd.ExecuteReader();
                        int s = 0;
    
                        while (rd.Read())
                        {
                            // CheckBox myCheckbox = new CheckBox();
                            //   myCheckbox.ID = "ckbExtraCharge" + i.ToString() + s.ToString();
                            // myCheckbox.Text = rd["Extra_Charge_Description"].ToString();
                            //  Page.FindControl("form1").Controls.Add(myCheckbox);
                            //  Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));
                            //  s++;
    
                            mycheckbox.ID = "chkblextracharge" + i.ToString() + s.ToString();
                            mycheckbox.Items.Add(rd["Extra_Charge_Description"].ToString());
                            Page.FindControl("form1").Controls.Add(mycheckbox);
    
                            if (mycheckbox.Items[s].Selected == true)
                            {  
                                myche.Add(mycheckbox.Items[s].Text);
                            }
                            s++;
                        }
                    }
                    catch (Exception ex)
                    { }
                }
            }    
    
         protected void btnProba_Click(object sender, EventArgs e)
                {
                    CheckWhatIsSelected();
                    if(myche.Count > 0)
                       lblProba.Text = myche[0];
                    else
                       lblProba.Text = "Non selected";
                }
    

    One other possible solution is to save the myche on viewstate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a WPF grid dynamically from code behind. This is going
I've got this code to dynamically create checkboxes and hook them up to a
I need to dynamically create textbox. This is my code, but with this I
This code attempts to dynamically switch the class of the StateContainer div from StateOne
i am using this code below to dynamically capture the name of the button
Basically I have a set of checkboxes that are dynamically created from view data
so this code here dynamically adds buttons to my wpf windows application. I cant
I have this code $(.delete2).click(function() { $('#load2').fadeIn(); } I have dynamically added item via
I want to dynamically create an image for a UITableViewCell which is basically a
I want to create the checkbox dynamically in the following code.The checkbox is getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.