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 4 3679
hi
u can use Me.Controls.Find for example
M.
"Randy" <sp***********@gmail.comwrote in message
news:11*********************@o11g2000prd.googlegro ups.com...
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
Randy,
You can supply the textbox's name as a key to the form's Controls
collection. For example:
CType (Me.Controls("tbItem" & intItemCount), TextBox).Text
But you also need to dynamically create the parameter place holders in the
Insert statement and the Parameters collection.
Kerry Moorman
"Randy" wrote:
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
"Kerry Moorman" <Ke**********@discussions.microsoft.comschrieb:
You can supply the textbox's name as a key to the form's Controls
collection. For example:
CType (Me.Controls("tbItem" & intItemCount), TextBox).Text
But you also need to dynamically create the parameter place holders in the
Insert statement and the Parameters collection.
However, note that this will only work if the controls are placed on the
form directly. Otherwise you'll have to use the 'Controls' collection of
the container the control belongs to.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Thanks everybody. Kerry's suggestion worked perfectly. Thanks for
the qualification, Herfried. All the controls are directly on the
form, so I didn't have to deal with that issue. As for dynamically
creating the parameter place holders, I simply added a
parameters.clear statement each time through the loop, which allowed
me to reuse the parameters without creating new ones.
Randy This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: muralidharan |
last post by:
WebForm1.aspx Code:
<%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI"
Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView
id="TreeView1" Height="520"...
|
by: Andrea Trevisan |
last post by:
That's a revival of a known thing I suppose.I hope it's useful.
My problem was:
I want to have a DataGrid with two Template columns: first with
TextBox,second with Button.I want to fire an event...
|
by: Patrick |
last post by:
This is a C# post.
I'm using VB.NET to create an add-in for the Google Sidebar, and have
implemented the OnDetailsView method.
NOTE : I've come across this same problem (error message) even in...
|
by: Patrick.O.Ige |
last post by:
I'm loading an Array below but getting the error "Object reference not set to
an instance saying 'ItemNumber = CType(Args.Item.FindControl("ItemNumber"),
TextBox).Text' is the error line.
I DON'T...
|
by: nate axtell |
last post by:
I'm looking for a way to refernce a control by a string that represents the
name of the control. I dynamically create some textboxes, labels, and
comboboxes each name by a loop iteration index. ...
|
by: Mori |
last post by:
I am using masterPage and I need to populate a textbox that is in a content
control with data from popup page that is not part of the master page. This
code works if no masterpage is involved. ...
|
by: b0b3rt |
last post by:
Hi. I've got a problem with object references in a second form in the application.
I'll just post the code of both forms.
The error is at the bottom of the post.
First form (With textbox)....
|
by: Brad Pears |
last post by:
I have the following code that references a "textbox" on a form. I want to
pass the value of this textbox to a stored procedure as a parameter. This
code is located on a different form obviously. I...
|
by: Ray Cassick |
last post by:
I can't believe why I had not noticed this before and why I never asked it
before...
When defining a string why is it not required to use the new keyword? Like:
Dim a As String = New String
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |