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 8929563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:41:58+00:00 2026-06-15T08:41:58+00:00

I have a form that a user selects lines to be set up with

  • 0

I have a form that a user selects lines to be set up with some information. I would like the user to be able to select all the lines they need and then do a separate insert statement for for each selection as a value for a parameter. For example user Select lines 2,3, and 25 . I need to do fire the insert statement 3 times and each time change the @line parameter to the next line selected. How would I do that? This is as far as I have gotten.

        protected void btn_test_Click(object sender, EventArgs e)
        {

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Insert into t_ap_line_setup  (line,date) values (@line,getdate())";
            //string strLines = "";

            // cmd.Parameters.Add("@line", SqlDbType.Int).Value = Convert.ToInt32(strLines);

            cmd.Connection = this.sqlConnection1;
            this.sqlConnection1.Open();

            for (int i = 0; i < lines_list.Items.Count; i++)
            {
                if (lines_list.Items[i].Selected)
                {
                    cmd.Parameters.Add("@line", SqlDbType.Int).Value = Convert.ToInt32(lines_list.Items[i].Text);
                    cmd.ExecuteNonQuery();
                }
            }
            this.sqlConnection1.Close();
        }

One more thing with the above logic I get two inserts and it only works with one selection. Could someone point me in the right direction?

  • 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-15T08:41:59+00:00Added an answer on June 15, 2026 at 8:41 am

    You can use a loop:

    var selectedLines = CheckBoxList1.Items.Cast<ListItem>()
                 .Where(li => li.Selected)
                 .Select(li => li.Text);
    using (var con = new SqlConnection(connectionString))
    using (var cmd = new SqlCommand("Insert into t_ap_line_setup  (line,date) values (@line,getdate())", con))
    {
        con.Open();
        foreach (string line in selectedLines)
        {
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@line", int.Parse(line));
            cmd.ExecuteNonQuery();
        }
    }
    

    Note that i have used the using-statement to ensure that the connection gets disposed(closed) as soon as i’m finished with it(even in case of exception). That is better than using a “global” connection(i hope it is not static). All the more in ASP.NET.

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

Sidebar

Related Questions

i have a form that has a multiple select drop down. a user can
So I have a form that accepts some input from a user that may
I currently have form that checks if a user has unsubmitted changes when they
I have a form that the user submits and returns a result, but it
So I have a form that the user fills out, when submitted an new
Here's what I want to do. I have a form that the user fills
I have a form that allows a user to import a spreadsheet. This spreadsheet
I have a form that enables a user to update multiple alert rules records
I have a user form that is working perfectly. The page makes use of
We have an entry form that gives the user a calendar drop down, but

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.