We really need to see how you set your Listbox up in order to answer this properly. Hopefully you used databinding to a datatable that had the display text and the student id in it, something like this:
Dim da as New DataAdapter("SELECT name, id FROM Student", conStr)
Dim dt as New DataTable
da.Fill(dt)
'please, name your controls something better than ListBox6!!
ListBox6.DisplayMember = "name"
ListBox6.DataSource = dt
This means your .SelectedItems
is a collection of DataRowView, from which we can retrieve the id:
'please, name your controls something better than Button1!!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each selected In ListBox6.SelectedItems.Cast(Of DataRowView)
UpdateLVe(selected("id").ToString())
Next
End Sub
Which will pass the ID downloaded when setting up the listbox, to the UpdateLve method
Private Sub UpdateLVe(ByVal studente As String)
'please, learn to use SQL Parameters and prevent SQL injection hacking!!
Dim sql As String = "UPDATE peace SET Votes = Votes + 1 WHERE studentid = ?"
Dim cmd = New OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("param1", studente)
Try
con.Open()
'you don't create a dataadapter for INSERT/UPDATE/DELETE queries
'ONLY make a dataadapter when you need to read/write datatable from/to a db
'this line of code is useless, you never use the adapter
'adapter = New OleDbDataAdapter(cmd)
'this line of code is useless, you never use the adapter
'adapter.UpdateCommand = con.CreateCommand()
'this line of code is useless, you never use the adapter
'adapter.UpdateCommand.CommandText = sql
'execute the command you made, 5 lines above
If cmd.ExecuteNonQuery() > 0 Then
MsgBox("Successfully Voted")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
And a final note:
Imports System.Data.OleDb
Public Class Form1
Dim conString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\jp\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\TestDb.mdb"
'consider not caching this at form level; make a new one when you need
Dim con As OleDbConnection = New OleDbConnection(conString)
'definitely don't cache this at form level; make a new one when you need
'Dim cmd As OleDbCommand
'definitely don't cache this at form level; make a new one when you need
'Dim adapter As OleDbDataAdapter
'definitely don't cache this at form level; make a new one when you need
'Dim dt As DataTable = New DataTable()
'you don't need to have a form level variable for this; you already have a form level ListBox6 that has this
'Private selectedIndices As New List(Of Integer)
Please, don't post the same question over and over - we're a free help service and that's the virtual equivalent of marching up to our desk when we're busy with something else, and punching us repeatedly in the face saying "answer me", "answer me", "answer me". If you want a service to be at your beck and call you'll need to pay for it on a freelancer hiring site
Student
is a single string, andSelectedItems
is a collection. – Joel Coehoorn