Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each - Next takes always the lower value 1

Status
Not open for further replies.

lunargirrl

Programmer
Jan 26, 2004
77
BR
I have 10 checkboxes in my CheckBoxList, each one has values that go from 1 to 10.
I cant find what is wrong in this for each - next, that is inserting in my database always the lower value that I have in my checkboxlist.

Dim item As ListItem
Dim value As Integer
ActualDate = FormatDateTime(Now(), DateFormat.ShortDate)


For Each item In Me.chkList.Items
If item.Selected = True Then

value = Convert.ToInt32(chkList.SelectedItem.Value)
strSql = "INSERT INTO T_web_modulos_usuarios (COD_USER, COD_MODULE, COD_ACTION, DAT_CADASTRO, DAT_ATZ) " & _
"VALUES ('" & Me.txtCodUser.Text & "', '" & value & "', '" & value & _
"','" & DataAtual & "', '" & DataAtual & "')"
Try
objSC.InsertData(strsql2, enuTipoBanco.Web)

Catch err As Exception
Response.Write("Error!")
End Try
End If
Next

Someone could help,
thank you!!
 
Sounds like it's zero-based (runs from 0..9)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I dont know exactly what you mean chiph.
For me it seems that the for-next isnt iterating correctly between the values, it counts how many checkboxes are checked, yes, but takes the value of the first checked checkbox, as the values are from 1 to 10, if i check the first (value = 1) and second (value = 2) checkboxes, it takes the value 1 and insert in the db (2 times, cuz 2 checkboxes are checked), or if i check the fifth (value = 5), sixth (value = 6) and nineth (value = 9) checkboxes, it inserts the value 5 (3 times) in the db.

I dont understand really what is causing this.
Thanks :)


 
I understand the problem now (I think).
This line of code:
Code:
value = Convert.ToInt32(chkList.SelectedItem.Value)
Which always uses the first selected item. It probably should be:
Code:
value = Convert.ToInt32(item.Value)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
What about this:
Code:
If item.Selected = True Then

Wouldn't that always set item.Selected to True, and thus always do the insert? It should be just
Code:
If item.Selected Then

Greetings,
Dragonwell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top