loop thru controls on form to add to parameter list

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
bonniemckeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RonaldBiemansCommented:
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
bonniemckeeAuthor Commented:
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
RonaldBiemansCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

bonniemckeeAuthor Commented:
When I try this I get 3 compile errors:

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




0
RonaldBiemansCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bonniemckeeAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.