R
Randy
My situation is that I have a form on which a number of textboxes and
comboboxes are added dynamically based on interaction with the user.
As these controls are added, they are given names based on how many
times the user has added the control. For example, the first textbox
is on the form at design time; its name is tbItem1. If the user adds
another textbox for items, its name is tbItem2, and so on. This all
works easily.
Where I have a problem is later on when I am trying to transfer the
text that the user places in these controls into the database. I am
using sql commands to do this. Here is my code (excerpted):
strSQL = "INSERT INTO Items (Item) VALUES @Item"
Dim cmdInsert As New SqlCommand(strSQL, cn)
for x = 1 to intItemCount
cmdInsert.Parameters.AddWithValue("@UOM", "tbItem" &
intItemCount & ".text")
cmdInsert.ExecuteNonQuery()
next
Unfortunately, this doesn't work. What winds up in the datatable is
the string value "tbItem1.text" (for the first iteration), not the
actual text in the textbox. I can easily see why this is happening,
but I don't have any idea how to modify this so that I can reference
the textbox based on the integer value of the loop, which is
imperative to this working as intended.
I've searched this board on this question and can't find a post that
resolves this question. At least not one the I understand well enough
to adapt to my application. Can anybody help?
Thanks,
Randy
comboboxes are added dynamically based on interaction with the user.
As these controls are added, they are given names based on how many
times the user has added the control. For example, the first textbox
is on the form at design time; its name is tbItem1. If the user adds
another textbox for items, its name is tbItem2, and so on. This all
works easily.
Where I have a problem is later on when I am trying to transfer the
text that the user places in these controls into the database. I am
using sql commands to do this. Here is my code (excerpted):
strSQL = "INSERT INTO Items (Item) VALUES @Item"
Dim cmdInsert As New SqlCommand(strSQL, cn)
for x = 1 to intItemCount
cmdInsert.Parameters.AddWithValue("@UOM", "tbItem" &
intItemCount & ".text")
cmdInsert.ExecuteNonQuery()
next
Unfortunately, this doesn't work. What winds up in the datatable is
the string value "tbItem1.text" (for the first iteration), not the
actual text in the textbox. I can easily see why this is happening,
but I don't have any idea how to modify this so that I can reference
the textbox based on the integer value of the loop, which is
imperative to this working as intended.
I've searched this board on this question and can't find a post that
resolves this question. At least not one the I understand well enough
to adapt to my application. Can anybody help?
Thanks,
Randy