bulk insert - form collections for novice

S

Sean

HI There,

I am making the transition from asp to asp .net, I am currenty writing an
application that requires a bulk insert from a webform into SQL server,
normally I would just create rows of html textboxes and then use the
Request.Form.Count property to collect each field. What I would like to know
is what is a good way of doing this in asp.net? Do I need to create an array
of textboxes or can I do this fro a datagrid?


Sean - Thanks in advance
 
S

Steve C. Orr [MVP, MCSD]

I'm not following you 100%, but it sounds like its probably a good job for
either a datagrid or a repeater. Repeaters are more manual but give you
additional flexibility. Datagrids are more automatic but have limitations.
 
S

Sean

HI Steve,

If I have a datagrid can I create empty textboxes in coumns? do you have any
code snippets?

Sean
 
A

Alan Ferrandiz Langley

I don't think a Repeater or Datagrid is a good option for this. You can use
ASP.NET TextBox controls to achieve what you want. Just create a regular
form with as many ASP.NET TextBox controls as you want and then you can use
this code to collect values from your ASP.NET Textbox controls. Be aware
that any control you create is a child of <FORM>
(System.Web.UI.HtmlControls.HtmlForm) so you will first have to find it as a
control of your Page class, once you have found it in the control collection
you have to iterate in the HtmlForm's control collection to find the TextBox
controls. You can then add the values to an ArrayList class (a dynamic
array) and retrieve each value by its index

Hope this helps
Alan Ferrandiz
MCT;MCDBA,MCSD
MSF Practitioner

Dim aValues As New ArrayList()
Dim iCount As Integer

If Page.IsPostBack Then
Dim ctlFormulario, ctlControl As Control
For Each ctlFormulario In Me.Controls
If TypeOf (ctlFormulario) Is
System.Web.UI.HtmlControls.HtmlForm Then
For Each ctlControl In ctlFormulario.Controls
If TypeOf (ctlControl) Is TextBox Then
aValues.Add(CType(ctlControl, TextBox).Text)
Response.Write(aValues(iCount) & "<BR>")
iCount += 1
End If
Next
End If
Next
End If
 
S

Sean

HI Alan,

Thanks for answering my question, just one other thing. If I need to create
rows of textboxes with the same name how can I acheive this? given that I
get errors if two controls have the same name.

Sean
 
A

Alan Ferrandiz Langley

I guess you mean controls with the same ID, which is the property that
identifies a control in the server. Well an ID has to be
unique as far as i know, but if you want to have something like a name that
is exactly the sameto your TextBox controls, you can add an attribute to the
Attributes collection of your TextBox controls.

The first time page loads you add an attribute to each TextBox control and
when the page is doing a postback you check in every control that attribute
you added. In my case i added an attribute called MyName and set it to the
value "MyId".

Alan Ferrandiz Langley
MCT, MCSD, MCDBA
MSF Practitioner

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim aValues As New ArrayList()
Dim iCount As Integer

If Page.IsPostBack Then ' page is doing postback
Dim ctlFormulario, ctlControl As Control
For Each ctlFormulario In Me.Controls
If TypeOf (ctlFormulario) Is
System.Web.UI.HtmlControls.HtmlForm Then
For Each ctlControl In ctlFormulario.Controls
If TypeOf (ctlControl) Is TextBox Then
If CType(ctlControl,
TextBox).Attributes.Item("MyName") = "MyId" Then
aValues.Add(CType(ctlControl, TextBox).Text)
Response.Write(CType(ctlControl,
TextBox).Attributes.Item("MyName") & "<BR>")
Response.Write(aValues(iCount) & "<BR>")
iCount += 1
End If
End If
Next
End If
Next
Else ' first time page loads, add same attribute to your TextBox
controls
TextBox1.Attributes.Add("MyName", "MyId")
TextBox2.Attributes.Add("MyName", "MyId")
TextBox3.Attributes.Add("MyName", "MyId")
TextBox4.Attributes.Add("MyName", "MyId")
TextBox5.Attributes.Add("MyName", "MyId")
End If

End Sub
 
S

sean

HI Alan,

I tried your code as it was and I could not get it to work at all, I change
some of the properties and it seems now to be working but iterates through
the controls a number of times. I have tried to exit the for loop when the
last control has been displayed but this is not working, what am I doing
wrong?

Sean - thanks in advance for your answer.


Dim aValues As New ArrayList()
Dim iCount As Integer
Dim counter as Integer



If Page.IsPostBack Then


'Dim myConnection as New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

'myConnection.Open()

Dim ctlFormulario, ctlControl As Control
For Each ctlFormulario In Me.Controls
'If TypeOf (ctlFormulario) Is
System.Web.UI.HtmlControls.HtmlForm Then
If TypeOf (ctlFormulario) Is
System.Web.UI.WebControls.TextBox Then
'For Each ctlControl In ctlFormulario.Controls
For Each ctlControl In ME.Controls

If TypeOf (ctlControl) Is TextBox Then
aValues.Add(CType(ctlControl, TextBox).Text)
response.write (CType(ctlControl, TextBox).ID & "<BR>")
Response.Write(" " & aValues(iCount) & "<BR>")

iCount += 1
counter += 1

If CType(ctlControl, TextBox).ID = "txtPrice6"
Then Exit For

'Dim myCommand As New SqlCommand("ListingAddFertigation",
myConnection)
'myCommand.CommandType = CommandType.StoredProcedure


End If
Next
End If
Next
End If

!--- form code snippet

<tr>
<td><asp:TextBox id="txtQuantity6" width="30" maxlength="30" runat="server"
/></td>
<td><asp:TextBox id="txtDescription6" width="150" style="height:55px;"
maxlength="250" runat="server" /></td>
<td><asp:TextBox id="txtSize6" width="30" maxlength="30" runat="server"
/></td>
<td><asp:TextBox id="txtMaterial6" width="150" style="height:55px;"
maxlength="250" runat="server" /></td>
<td><asp:TextBox id="txtPrice6" width="30" maxlength="30" runat="server"
/></td>
</tr>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top