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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:04:33+00:00 2026-06-12T13:04:33+00:00

I have a loop that takes values from textboxes and dropdowns and uses them

  • 0

I have a loop that takes values from textboxes and dropdowns and uses them to update a an SQL table. I need to be able to rename the parameters and give them values based on the variable that the loop counts to. Example: if loop goes from 0 to 2 then textname_0 will go to @paramname_0 and textname_1 goes to @paramname_1 and so on.

I have written the code below but it generates an error which intimates the first parameter can’t be found so the syntax is clearly wrong.

    Protected Sub Insert_To_Tables()

    Try


        Dim con As SqlConnection = New SqlConnection()
        Dim connectionString As String = "Data Source=wolf;Initial Catalog=Seat_Planner_2013;Integrated Security=True;Integrated Security=True"
        Using cn As New SqlConnection(connectionString)

            For i As Int32 = 0 To number_of_tickets_Ddl.SelectedValue

                cn.Open()
                Dim cmd As New SqlCommand()
                cmd.CommandText = "INSERT INTO tables ([table_num], [seat], [available], [ticket_num], [seat_title], [seat_firstname], [seat_surname], [booking_ref],[booked_by]) VALUES (@table_num_ & i, @seat_ & i, @available_ & i, @ticket_num_ & i, @seat_title_ & i, @seat_firstname_ & i, @seat_surname_ & i, @booking_ref, @booked_by) WHERE ([table_num] = @table_num) AND ([seat] = @seat_ & i)"

                cmd.Parameters.Add("@table_num", SqlDbType.SmallInt).Value = ddltable.SelectedValue
                cmd.Parameters.Add("@seat_" & i, SqlDbType.NChar).Value = CType(Me.FindControl(("DDLSeat_") & i), DropDownList).SelectedValue
                cmd.Parameters.Add("@available", SqlDbType.NChar).Value = "No"
                cmd.Parameters.Add("@ticket_num_" & i, SqlDbType.NChar).Value = CType(Me.FindControl(("TicketNo_TextBox_") & i), TextBox).Text.Trim
                cmd.Parameters.Add("@seat_title_" & i, SqlDbType.NChar).Value = CType(Me.FindControl(("Title_TextBox_") & i), TextBox).Text.Trim
                cmd.Parameters.Add("@seat_firstname_" & i, SqlDbType.NChar).Value = CType(Me.FindControl(("FirstName_TextBox_") & i), TextBox).Text.Trim
                cmd.Parameters.Add("@seat_surname_" & i, SqlDbType.NChar).Value = CType(Me.FindControl(("Surname_TextBox_") & i), TextBox).Text.Trim
                cmd.Parameters.Add("@booking_ref", SqlDbType.NChar).Value = booking_ref_LBL.Text
                cmd.Parameters.Add("@booked_by", SqlDbType.NChar).Value = CType(Me.FindControl(("TBoxFull_name_0")), TextBox).Text.Trim

                cmd.Connection = cn
                cmd.ExecuteNonQuery()
                cn.Close()

            Next

        End Using

        DB_error1.Text = " Insert to tables success. "

    Catch ex As Exception

        DB_error1.Text = "Fail "
        DB_error1.Text = DB_error1.Text + ex.Message.ToString
    Finally

    End Try
  • 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-12T13:04:34+00:00Added an answer on June 12, 2026 at 1:04 pm

    The SQL doesn’t have dynamic parameters names – they are fixed. For example – you have a @table_num parameter in the SQL query (CommandText). Additionally, you have & i inside the string, making this invalid SQL.

    Just use (@table_num, @seat, @available, ...

    The added parameters must match these names, so concatenating is the wrong thing to do either in the SQL or when adding the parameters.

    Just drop the concatenation and things should work as you expect.


    For efficiency, I suggest you read up on table valued parameters.

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

Sidebar

Related Questions

I have a loop that builds a table with camera names. In that table
I have an input that takes tag names, with a space between them. Then
I have a loop that enters each non-empty text field into a database: foreach
I have a loop that finds duplicate lines in a .ini file. I can
I have a loop that runs through a variety of websites and I'd like
I have a loop that runs for approx. 25 minutes i.e 1500 seconds. [100
I have a loop that calculates the similarity between two documents. It collects all
I have a loop that goes through all the news items we have on
I have a loop that works but not when I try and use it
I have a loop that can look like this: For Each article In artAll

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.