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
Kevin DavisPosted May 7, 2012, 5:30 PM
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.
VulpesPosted May 7, 2012, 4:49 PM
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 DavisPosted May 7, 2012, 4:29 PM
VulpesPosted May 7, 2012, 4:17 PM
Kevin DavisPosted May 7, 2012, 3:21 PM
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....
VulpesPosted May 7, 2012, 2:44 PM
Kevin DavisPosted May 7, 2012, 2:42 PM
VulpesPosted May 7, 2012, 12:57 PM
Is it possible that there could be a couple of rows in the ListView with the same ID column?
Kevin DavisPosted May 7, 2012, 12:37 PM
Kevin DavisPosted May 7, 2012, 12:29 PM
VulpesPosted May 7, 2012, 11:35 AM
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.