I have a listview with a few columns, and I'm trying to choose all items and add each of them to my MYSQL database online. However, when I try to run the script, it will either only update the first record, or the last record, but never all the records. Any help would be appreciated. Thank you.
Dim strsql As String
For i = 0 To ListView1.Items.Count - 1
Dim a As String = ListView1.SelectedItems(i).SubItems(15).Text---------------------------Customer Name Column
Dim b As Integer = CInt(ListView1.SelectedItems(i).Text)------------------------------------Id Number of Customer
'strsql = "Update orders SET Name = '" & ListView1.SelectedItems(i).SubItems(15).Text & "' where ID = '" & ListView1.SelectedItems(i).Text & "'"
strsql = "Update orders SET Name = '" & a & "' where ID = '" & b & "'"
Dim da As New MySqlDataAdapter(strsql, CONNECTION)
Next
da.Fill(ds)
I've tried a couple different ways of doing this, and it "partially" works either way. I also get a error stating that "the integer i will not be inferred as it is bound to a filed in an enclosing scope."
Thanks again for any help.
Zcast
Loading

VulpesRank #55 · 29.2K reputationPosted May 7, 2012, 7:35 AM EDT
For i = 0 To ListView1.Items.Count - 1
be:
For i = 0 To ListView1.SelectedItems.Count - 1
One other thing I noticed is that you're putting single quotes around the integer 'b' in the Update command. This will be OK as long as ID is a varchar or similar in the database but not if it's an integer.
Kevin Davis52 reputationPosted May 7, 2012, 8:29 AM EDT
Kevin Davis52 reputationPosted May 7, 2012, 8:37 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 7, 2012, 8:57 AM EDT
Is it possible that there could be a couple of rows in the ListView with the same ID column?
Kevin Davis52 reputationPosted May 7, 2012, 10:42 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 7, 2012, 10:44 AM EDT
Kevin Davis52 reputationPosted May 7, 2012, 11:21 AM EDT
Dim a As String = ListView1.SelectedItems(i).SubItems(15).Text
Dim b As Integer = CInt(ListView1.SelectedItems(i).Text)
strsql = "Update orders SET Name = '" & a & "' where ID = ' " & b & ""
Dim da As New MySqlDataAdapter(strsql, CONNECTION)
da.Fill(ds)
Next
The weird thing is it very sporadic in which records it will update. I just tried it again, and it updated records 4,5,7,8 but skipped record 6. Tried it again, and it updated records 4,5,6, and 7, but not 8. I'm super confused....
VulpesRank #55 · 29.2K reputationPosted May 7, 2012, 12:17 PM EDT
Kevin Davis52 reputationPosted May 7, 2012, 12:29 PM EDT
VulpesRank #55 · 29.2K reputationPosted May 7, 2012, 12:49 PM EDT
The documentation for the MySqlDataAdapter is linked to below and I'm reasonably certain that you can only pass a SELECT command to the constructor. The UPDATE command needs to be assigned to the UpdateCommand property:
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqldataadapter.html#connector-net-examples-mysqldataadapter-updatecommand
Kevin Davis52 reputationPosted May 7, 2012, 1:30 PM EDT
Private sub UPDATE()
For Me.i = 0 To ListView1.SelectedItems.Count - 1
strSQL = "Update orders set Name = '" & ListView1.SelectedItems(i).SubItems(15).Text & "' where ID = " & Val(ListView1.SelectedItems(i).Text & "")
Dim da As New MySqlDataAdapter(strSQL, CONNECTION)
da.Fill(ds)
Next
end sub
Thanks for all your help Vulpes. I appreciate it very much.