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

  • Home
  • SEARCH
  • 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 8168093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:24:29+00:00 2026-06-06T20:24:29+00:00

I am trying to use a for-if loop to run through a few CheckBoxList

  • 0

I am trying to use a for-if loop to run through a few CheckBoxList controls, to see if a checkbox is checked. If checked, it will run an INSERT statement to the DB.

My first CheckBoxList (stapleCheckBoxList) works as it should (Eg. If i selected all 4 checkboxes, the corresponding items will be inserted into the db). However for my second CheckBoxList (seafoodCheckBoxList), it seems to only do an INSERT on the first checkbox that is selected. Could anyone identify whats wrong with my codes below?

protected void btnTSubmit_Click(object sender, EventArgs e)
    {
        int i;
        int intOrderNo = (int)Session["sOrderNo"];
        int Qty;
        int UnitPrice;
        string ProdId;

        if (txtPaxQty.Text != String.Empty)
        {
            UnitPrice = 12;
            Qty = int.Parse(txtPaxQty.Text);
            ProdId = "PK001";

            OleDbConnection DBconn2 = new OleDbConnection();
            DBconn2.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
                        + Server.MapPath("~/App_Data/ParissDB.mdb");
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = DBconn2;
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
            + "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
            cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
            cmd.Parameters.AddWithValue("@ProductId", ProdId);
            cmd.Parameters.AddWithValue("@Qty", Qty);
            cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
            DBconn2.Open();
            cmd.ExecuteNonQuery();
            DBconn2.Close();
        }

        for (i = 0; i < stapleCheckBoxList.Items.Count; i++)
        {
            String str = "";

            if (stapleCheckBoxList.Items[i].Selected)
            {

                str = stapleCheckBoxList.Items[i].Text;

                OleDbConnection DBconn = new OleDbConnection();
                DBconn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
                            + Server.MapPath("~/App_Data/ParissDB.mdb");

                Qty = 0;
                UnitPrice = 0;

                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = DBconn;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
                + "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";

                //Staple

                if (stapleCheckBoxList.Items[i].Text == "Yang Chow Fried Rice")
                {
                    str = "ST001";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }

                if (stapleCheckBoxList.Items[i].Text == "Pineapple Fried Rice")
                {
                    str = "ST002";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }

                if (stapleCheckBoxList.Items[i].Text == "Mee Goreng")
                {
                    str = "ST003";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }

                if (stapleCheckBoxList.Items[i].Text == "Fried Udon with Seafood")
                {
                    str = "ST004";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }


                //Response.Redirect("Delivery.aspx");
            }
        }

        for (i = 0; i < seafoodCheckBoxList.Items.Count; i++)
        {
            String str2 = "";

            if (seafoodCheckBoxList.Items[i].Selected)
            {

                str2 = seafoodCheckBoxList.Items[i].Text;

                OleDbConnection DBconn = new OleDbConnection();
                DBconn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
                            + Server.MapPath("~/App_Data/ParissDB.mdb");

                Qty = 0;
                UnitPrice = 0;

                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = DBconn;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
                + "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";

                //Seafood

                if (seafoodCheckBoxList.Items[i].Text == "Butter Prawns")
                {
                    str2 = "SE002";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str2);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }

                if (seafoodCheckBoxList.Items[i].Text == "Cereal Prawns")
                {
                    str2 = "SE001";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str2);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }

                if (seafoodCheckBoxList.Items[i].Text == "Thai Style Prawns")
                {
                    str2 = "SE003";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str2);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }

                if (seafoodCheckBoxList.Items[i].Text == "BBQ Squid")
                {
                    str2 = "SE004";
                    cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
                    cmd.Parameters.AddWithValue("@ProductId", str2);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
                    DBconn.Open();
                    cmd.ExecuteNonQuery();
                    DBconn.Close();
                }

                Response.Redirect("Delivery.aspx");
            }

        }
  • 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-06T20:24:30+00:00Added an answer on June 6, 2026 at 8:24 pm

    The line

    Response.Redirect("Delivery.aspx");
    

    is inside your for loop. So the first time it goes through the loop, you are already redirected. Just move the redirect to after the for loop.

    And you should SERIOUSLY think about refactoring your code. There’s too much repeated stuff that you can convert into methods, among other things.

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

Sidebar

Related Questions

I'm trying to use Linq to loop through all fonts in the %windir%\Fonts folder
I'm trying to use openmp to multithread a loop through std::set. When I write
I'm trying to use a for line in data: loop to assign the first
How can I use a batch file to loop through non-integer values? I'm trying
I am trying to program a simple loop to run through the user selected
I am currently trying to design a for loop to iterate through any 'checked'
I am trying to use for..in loop to access the description key/value within 'Event'
I'm trying to use a foreach loop for an array of objects. Inside of
I'm trying to use the foreach loop in an Ant script but I get
I'm trying to use a cursor loop in MYSQL, but it's not working. I've

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.