473,386 Members | 2,050 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,386 software developers and data experts.

Save Record With "For Each ctrl In Me.controls" Statement

I'm trying to write a piece of code that will programatically save a
record automatically without me having to add a new ' Row.Item("ADD1")
= txtAdd1.Text.Trim.ToUpper ' type command each time I add a new
textbox. I've named all my textboxes in the following format txtAdd1
and I've named all my field in the SQL table the same way minus the
txt at the beginning.

This code scans the form for textboxes. If it finds a text box that
is not disabled, then it is supposed to save the text, typed by the
user, into the SQL table. The only problem is that the text the user
typed in is not being saved to the table, such as "John" in my
FirstName field.

I tried adding a label (Label2) to my form so I could see what might
be going on, but no text is displayed in Label2 after the postback.

Does anyone have any suggestions?

Thanks,
crjunk

Here is what I have so far:

Dim ctrl As Control
Dim ctlNameText As TextBox
Dim ctlNameDDL As DropDownList

For Each ctrl In Me.Controls
If (TypeOf (ctrl) Is TextBox) Then
ctlNameText = ctrl
If ctlNameText.Enabled = True Then
Dim FieldName As String
FieldName = ctrl.ID
Row.Item(Mid(Trim(FieldName), 4, Len(FieldName))) =
ctlNameText.Text.Trim.ToUpper + ""
Label2.Text = Label2.Text.Trim + " " +
ctlNameText.Text.Trim.ToUpper
End If
End If
Next
Nov 18 '05 #1
2 2857
make it a sub as you need to call it recursively as the form is a child control in the collection which has it's own collection of controls (preumably where your textboxes are) ...

I use this (modify it as needed):

Private Sub IterateThroughChildren(ByVal parent As Control)
Dim c As Control
Dim lb As LinkButton
Dim strToolTip As String = "Click to Save all data and proceed to this section."
Dim strConfirmText As String = "Do you wish to save your data first?"

For Each c In parent.Controls
If InStr(LCase(c.GetType.ToString), "linkbutton") Then
lb = CType(c, LinkButton)
If intContID = 0 Then
lb.Enabled = False
Else
lb.ToolTip = strToolTip
lb.Attributes.Add("onclick", "javascript: doSaveFirst('" & lb.ID & "','" & strConfirmText & "');")
lb.CommandName = "true"
lb.CausesValidation = False
End If
End If

If c.Controls.Count > 0 Then
IterateThroughChildren(c)
End If
Next c
End Sub

"crjunk" <cr****@earthlink.net> wrote in message news:e4**************************@posting.google.c om...
I'm trying to write a piece of code that will programatically save a
record automatically without me having to add a new ' Row.Item("ADD1")
= txtAdd1.Text.Trim.ToUpper ' type command each time I add a new
textbox. I've named all my textboxes in the following format txtAdd1
and I've named all my field in the SQL table the same way minus the
txt at the beginning.

This code scans the form for textboxes. If it finds a text box that
is not disabled, then it is supposed to save the text, typed by the
user, into the SQL table. The only problem is that the text the user
typed in is not being saved to the table, such as "John" in my
FirstName field.

I tried adding a label (Label2) to my form so I could see what might
be going on, but no text is displayed in Label2 after the postback.

Does anyone have any suggestions?

Thanks,
crjunk

Here is what I have so far:

Dim ctrl As Control
Dim ctlNameText As TextBox
Dim ctlNameDDL As DropDownList

For Each ctrl In Me.Controls
If (TypeOf (ctrl) Is TextBox) Then
ctlNameText = ctrl
If ctlNameText.Enabled = True Then
Dim FieldName As String
FieldName = ctrl.ID
Row.Item(Mid(Trim(FieldName), 4, Len(FieldName))) =
ctlNameText.Text.Trim.ToUpper + ""
Label2.Text = Label2.Text.Trim + " " +
ctlNameText.Text.Trim.ToUpper
End If
End If
Next

Nov 18 '05 #2
Thanks for your suggestion. I ended up figuring out that I needed to
change my For Each statement. Here is what I did to correct the
problem:

Dim ctrl As Control
Dim ctlNameText As TextBox

For Each ctrl In Page.FindControl("Form1").Controls
If TypeOf (ctrl) Is TextBox Then
ctlNameText = ctrl
If (ctlNameText.Enabled = True) And (ctlNameText.Visible =
True) Then
Dim FieldName As String
FieldName = ctrl.ID
Row.Item(Mid(Trim(FieldName), 4, Len(FieldName))) =
ctlNameText.Text.Trim.ToUpper + ""
End If
End If
Next

Thanks for your help!

"Thomas Dodds" <th*********@hotmail.com> wrote in message news:<uU**************@TK2MSFTNGP15.phx.gbl>...
make it a sub as you need to call it recursively as the form is a child
control in the collection which has it's own collection of controls
(preumably where your textboxes are) ...

I use this (modify it as needed):

Private Sub IterateThroughChildren(ByVal parent As Control)
Dim c As Control
Dim lb As LinkButton
Dim strToolTip As String = "Click to Save all data and proceed to
this section."
Dim strConfirmText As String = "Do you wish to save your data
first?"

For Each c In parent.Controls
If InStr(LCase(c.GetType.ToString), "linkbutton") Then
lb = CType(c, LinkButton)
If intContID = 0 Then
lb.Enabled = False
Else
lb.ToolTip = strToolTip
lb.Attributes.Add("onclick", "javascript: doSaveFirst('"
& lb.ID & "','" & strConfirmText & "');")
lb.CommandName = "true"
lb.CausesValidation = False
End If
End If

If c.Controls.Count > 0 Then
IterateThroughChildren(c)
End If
Next c
End Sub

"crjunk" <cr****@earthlink.net> wrote in message
news:e4**************************@posting.google.c om...
I'm trying to write a piece of code that will programatically save a
record automatically without me having to add a new ' Row.Item("ADD1")
= txtAdd1.Text.Trim.ToUpper ' type command each time I add a new
textbox. I've named all my textboxes in the following format txtAdd1
and I've named all my field in the SQL table the same way minus the
txt at the beginning.

This code scans the form for textboxes. If it finds a text box that
is not disabled, then it is supposed to save the text, typed by the
user, into the SQL table. The only problem is that the text the user
typed in is not being saved to the table, such as "John" in my
FirstName field.

I tried adding a label (Label2) to my form so I could see what might
be going on, but no text is displayed in Label2 after the postback.

Does anyone have any suggestions?

Thanks,
crjunk

Here is what I have so far:

Dim ctrl As Control
Dim ctlNameText As TextBox
Dim ctlNameDDL As DropDownList

For Each ctrl In Me.Controls
If (TypeOf (ctrl) Is TextBox) Then
ctlNameText = ctrl
If ctlNameText.Enabled = True Then
Dim FieldName As String
FieldName = ctrl.ID
Row.Item(Mid(Trim(FieldName), 4, Len(FieldName))) =
ctlNameText.Text.Trim.ToUpper + ""
Label2.Text = Label2.Text.Trim + " " +
ctlNameText.Text.Trim.ToUpper
End If
End If
Next

--

Nov 18 '05 #3

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

Similar topics

4
by: babylon | last post by:
I created a class which inherits from UserControl.. However, I can't find it in the "My User Controls" tab in designer view... How can i add it back myself? thx...
8
by: Lian | last post by:
Hi all, It is a newbie's question about html tag "img". The attributes "title" and "alt" for "img" seems having the same function. So what is the main difference between them? Can i use them at...
6
by: alederer | last post by:
Hallo! I have a table tstest(ts char(13) for bit data constraint a unique). This column is filled in a trigger with generate_unique(). In a application (CLI), I have the values of this...
1
by: deko | last post by:
This sub pulls Outlook Appointments into a table. The problem is I want to limit the import to Location = Boston I'm not sure how to code that into the For... To loop - As it is now, I get the...
5
by: TJS | last post by:
trying to display pdf file in browser fails on this line: Response.ContentType = "application/pdf" getting an error about no declaration found for "response" what declaration is needed ???
0
by: Markus Rytterkull | last post by:
Hi! Is it possible to control where controls that are not visible (datasource etc) are positioned in design view? I'm using masterpages with two contentplaceholdes (they alone make the page...
0
by: martin leer | last post by:
Hi all. I got the following error: OLE DB provider "MSDAORA" for linked server "infonet" returned message "Oracle client and networking components were not found. These components are...
1
by: Rahul Babbar | last post by:
Hi, I ran the scripts in a file from Command Line Processor and it gave the error for all the constraints being added, but not the indexes being added. For a simple statement like Alter...
3
by: Rahul Babbar | last post by:
Hi, I had the following doubts about the "For Read Only" clause. 1. How does a "for Read only" clause improve the performance? 2. How does a "for Read only" clause compare with "With UR"...
8
by: Ratko | last post by:
Say you have something like this: for item in myList: del item Would this actually delete the item from the list or just decrement the reference counter because the item in myList is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.