473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reference a Textbox using a String

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

Jun 8 '07 #1
4 3679
Mex
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

Jun 8 '07 #2
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

Jun 8 '07 #3
"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/>

Jun 8 '07 #4
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

Jun 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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"...
0
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...
0
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...
3
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...
4
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. ...
4
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. ...
3
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)....
9
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...
21
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 ...
1
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.