Link to home
Start Free TrialLog in
Avatar of bonniemckee
bonniemckee

asked on

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
Avatar of RonaldBiemans
RonaldBiemans

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


Avatar of bonniemckee

ASKER

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
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
When I try this I get 3 compile errors:

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




ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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