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("@major 6", 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
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("@major
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
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("@major 6", 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("whatdo Idohere?", SqlDbType. Char,x.Max length).Va lue = x.Value
End If
next
cmd.Parameters.Add("@major
Could I do something like this?:
for each x as control in me.controls
If x.GetType Is GetType(TextBox) Then
cmd.Parameters.Add("whatdo
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.Ma xlength).V alue = 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.Max length).Va lue = x.Value
End If
next
for each x as control in me.controls
If x.GetType Is GetType(TextBox) Then
cmd.Parameters.Add("@" & x.Name,SqlDbType.Char,x.Ma
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.Max
End If
next
ASKER
When I try this I get 3 compile errors:
Maxlength/Name/Value are not a member of System.Web.UI.Control
Maxlength/Name/Value are not a member of System.Web.UI.Control
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(paranam e, SqlDbType.Char, mxlength).Value = CType(x.Controls(i), HtmlInputText).Value
End If
Next
End If
Next
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(paranam
End If
Next
End If
Next
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