For Each Loop - Variable names must be unique

  • Thread starter Thread starter pvong
  • Start date Start date
P

pvong

I'm trying to do this in VB.NET

I have 10 TextBoxes in a Panel. All I want to do is loop through each box
and see if it's empty. If it's not empty, do the Insert. The first Insert
works but it fails at the next TextBox because it needs a unquie variable.
Can someone help me figure out a way so the variable will be unique through
each loop?

For Each ctrl As Control In Panel3.Controls
If TypeOf ctrl Is TextBox Then
If Not CType(ctrl, TextBox).Text = String.Empty Then
ds.InsertCommand = "INSERT INTO LpCC2 (CcID, LpClientsID) VALUES
(@CcID4,@LpClientsID4)"
ds.InsertParameters.Add("CcID4", "10")
ds.InsertParameters.Add("LpClientsID4", CType(ctrl, TextBox).Text)
ds.Insert()
End If
End If
Next



The variable name '@CcID4' has already been declared. Variable names must be
unique within a query batch or stored procedure.



Thanks!
 
if you put this code in after this line

ds.InsertParameters.Add("LpClientsID4", CType(ctrl, TextBox).Text)
dim paramCount as int = ds.Parameters.Count
Console.Writeline(paramCount) '' the first loop will be one, then the second
loop will be two, etc, etc

or something like that.

You are adding parameters over and over.

You should also look creating a datalayer.
Create a bulk insert/update (instead of looping over and over).


Your code is a mixture of persentation ,business and datalayer in one. Very
sloppy coding.
 
Sloan-Thanks for the reply. I'm a newbie at this programming stuff. I
guess I need to spend some time to learn about DataLayers. In the mean
time, I got an error using your example. This is now my new code.

For Each ctrl As Control In Panel3.Controls
If TypeOf ctrl Is TextBox Then
If Not CType(ctrl, TextBox).Text = String.Empty Then
ds.InsertCommand = "INSERT INTO LpCC2 (CcID,
LpClientsID) VALUES(@CcID4,@LpClientsID4)"
ds.InsertParameters.Add("CcID4", "10")
Dim paramCount As Integer = ds.Parameters.Count
Console.WriteLine(paramCount)
ds.InsertParameters.Add("LpClientsID4", CType(ctrl,
TextBox).Text)
ds.Insert()
End If
End If
Next

In your line = ds.Parameters.Count, I get the error message of Error
'Parameters' is not a member of 'System.Web.UI.WebControls.SqlDataSource'.
 
Since you didn't provide the object type for "ds", I don't know exactly
which properties are available.

Basically, you're adding the parameters over and over............... You
should only add it ONCE.

Highlight the "ds" and do a "Add Watch" and you'll find properties for it.
Maybe there is a Parameters property, and you can see the count.
I don't know,


Try

ds.InsertParameters.Count

You're gonna have to fish a little bit yourself.
 
pvong was thinking very hard :
I'm trying to do this in VB.NET

I have 10 TextBoxes in a Panel. All I want to do is loop through each box
and see if it's empty. If it's not empty, do the Insert. The first Insert
works but it fails at the next TextBox because it needs a unquie variable.
Can someone help me figure out a way so the variable will be unique through
each loop?

For Each ctrl As Control In Panel3.Controls
If TypeOf ctrl Is TextBox Then
If Not CType(ctrl, TextBox).Text = String.Empty Then
ds.InsertCommand = "INSERT INTO LpCC2 (CcID, LpClientsID) VALUES
(@CcID4,@LpClientsID4)"
ds.InsertParameters.Add("CcID4", "10")
ds.InsertParameters.Add("LpClientsID4", CType(ctrl, TextBox).Text)
ds.Insert()
End If
End If
Next



The variable name '@CcID4' has already been declared. Variable names must be
unique within a query batch or stored procedure.



Thanks!

Move the InsertCommand and the parameters out of the loop (you are
assigning the same value over and over). Place it before the 'For
Each'.

The value of LpClientsID4 changes, so add the parameter without a value
and store the resulting 'int' in some variable.
Dim paramIndex as Integer
paramIndex = ds.InsertParameters.Add("LpClientsID4")

Within the loop you should not add a new parameter, but change the
value of the existing one.
ds.InsertParameters(paramIndex).Value = CType(ctrl, TextBox).Text

Note: I'm guessing at the exact VB syntax, but I think it's close.

Hans Kesting
 
Thanks Hans!
After reading your response, I came up with a simple solution. This is what
I ended up using and it worked perfectly.

ds.InsertCommand = "INSERT INTO LpCC2 (CcID, LpClientsID) VALUES
(@CcID4,@LpClientsID4)"

ds.InsertParameters.Add("LpClientsID4", "")

ds.InsertParameters.Add("CcID4", Session("CcID"))

For Each ctrl As Control In Panel3.Controls

If TypeOf ctrl Is TextBox Then

If Not CType(ctrl, TextBox).Text = String.Empty Then

ds.InsertParameters("LpClientsID4").DefaultValue = CType(ctrl, TextBox).Text

ds.Insert()

End If

End If

Next
 
i have more or less same kind of problem

here is the code

Dim arr() As String
Dim record As Int32
record = 0
For Each div As System.Web.UI.Control In plholder.Controls

If div.[GetType]().Name = "HtmlGenericControl" Then
Dim hml As HtmlGenericControl = DirectCast(div, HtmlGenericControl)


Dim str12 As String
str12 = hml.TagName
str12 = hml.ID

For Each table As System.Web.UI.Control In div.Controls

If table.[GetType]().Name = "HtmlTable" Then

Dim htab As HtmlTable = DirectCast(table, HtmlTable)

Dim str13 As String
str13 = htab.TagName
str13 = htab.ID

For Each row As System.Web.UI.Control In table.Controls

Dim hrow As HtmlTableRow = DirectCast(row, HtmlTableRow)

Dim str14 As String
str14 = hrow.TagName
str14 = hrow.ID



For Each cell As System.Web.UI.Control In row.Controls

Dim hcell As HtmlTableCell = DirectCast(cell, HtmlTableCell)
Dim str15 As String
str15 = hcell.TagName
str15 = hcell.ID

For Each textbox As System.Web.UI.Control In cell.Controls

If textbox.GetType Is GetType(WebControls.TextBox) Then


Dim tbox As TextBox = CType(textbox, System.Web.UI.Control)
Dim str16, str17 As String
str16 = tbox.TextMode
str16 = tbox.ID
str17 = tbox.Text

If tbox.Text <> String.Empty Then
arr(record) = str17
record = record + 1
MsgBox(arr(record))
End If
End If
Next 'textbox
Next 'cell
Next 'row


End If
Next 'table

'here you can access inner controls of html

End If
Next 'div
i have bit tricky kind or problem. i need to get values of each textbox in loop with different names,i have three row (name,telephone,email)and off course three textbox ,can any one help how to get in loop ,i need to insert them in databse

i have update loop like this
 
Back
Top