?
Solved

loop thru controls on form to add to parameter list

Posted on 2004-11-21
6
Medium Priority
?
260 Views
Last Modified: 2010-04-23
I am new to VB.net coming from Visual Foxpro.  I am loading a SQL database with a big vb.net web form.  I connect to SQL with SQLconnect and  and Insert statement executed with  ExecuteReader.

 I have more than 50 controls on the form and some pretty ugly code that fills the parameters to send to the sQL insert command:

cmd.Parameters.Add("@major6", SqlDbType.Char, 25).Value = Major6Text.Value

for 50 fields is not pretty!

Is there a way to loop thru all controls on my form (they are all server side controls with an id) ?

thanks

Bonnie
0
Comment
Question by:bonniemckee
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12642666
to loop thru all the controls on the form use something like

for each x as control in me.controls
  'do your thing here
next

although this code loops through everything including labels etc.. if you just want the textboxes do something like

for each x as control in me.controls
   If x.GetType Is GetType(TextBox) Then
       'do your thing here
   End If
next


0
 

Author Comment

by:bonniemckee
ID: 12646672
Thanks! that helps part way (I really am a rookie here).  Can you help me thru the other piece too of accessing the properties of the control?  If the Textbox is called Major6Text this is what I use now:

cmd.Parameters.Add("@major6", SqlDbType.Char, 25).Value = Major6Text.Value

Could I do something like this?:

for each x as control in me.controls
   If x.GetType Is GetType(TextBox) Then
       cmd.Parameters.Add("whatdoIdohere?",SqlDbType.Char,x.Maxlength).Value = x.Value
   End If
next
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12648241
maybe something like this

for each x as control in me.controls
   If x.GetType Is GetType(TextBox) Then
       cmd.Parameters.Add("@" & x.Name,SqlDbType.Char,x.Maxlength).Value = x.Value
   End If
next

or you can put a name in the tag of the textboxes

for each x as control in me.controls
   If x.GetType Is GetType(TextBox) Then
       cmd.Parameters.Add("@" & x.tag,SqlDbType.Char,x.Maxlength).Value = x.Value
   End If
next
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bonniemckee
ID: 12648562
When I try this I get 3 compile errors:

Maxlength/Name/Value are not a member of System.Web.UI.Control




0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 300 total points
ID: 12652225
oeps, sorry about that,

        For Each x As Control In Me.Controls
            If x.GetType Is GetType(TextBox) Then
                cmd.Parameters.Add("@" & CType(x, TextBox).text, SqlDbType.Char, CType(x, TextBox).Maxlength).Value = CType(x, TextBox).text
            End If
        Next
0
 

Author Comment

by:bonniemckee
ID: 12658510
Thanks. This got me started.  I am actually using HTMLInputText so this is the code that I ended up using.  It looks like I need to drill down a little further to get at the controls:

For Each x As Control In Me.Controls
                        Dim i As Integer
                        If x.Controls.Count > 0 Then
                            For i = 0 To x.Controls.Count - 1
                                If x.Controls(i).GetType Is GetType(HtmlInputText) Then
                                    mxlength = CType(x.Controls(i), HtmlInputText).MaxLength
                                    If mxlength < 0 Then
                                        mxlength = 25
                                    End If
                                    ' ? puts 'text' at end: id not name
                                    paraname = "@" & CType(x.Controls(i), HtmlInputText).Name
                                    paraname = Left(paraname, Len(paraname) - 4)
                                    cmd.Parameters.Add(paraname, SqlDbType.Char, mxlength).Value = CType(x.Controls(i), HtmlInputText).Value
                                End If
                            Next
                        End If
                    Next
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question