Good night

I would like to know how i can do to insert in a table of mysql some itens that i select in a Checkedlistbox.
But the Checkedlistbox as very itens.

Private Sub btnguardar3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar3.Click

        Dim data_adapter As Odbc.OdbcDataAdapter
        Dim data_table As New DataTable
        Dim i As Integer
        Dim j As Integer
        Dim nome As String

        Call ligar()
        
        i = 0


        For i = 0 To CheckedListBox1.CheckedItems.Count - 1


            nome = CheckedListBox1.GetItemText(CheckedListBox1.SelectedItems.Item(i))
            data_adapter = New Odbc.OdbcDataAdapter("select id_disc from disciplinas where disc_nome = '" & nome & "'", con)
            data_adapter.Fill(data_table)
            i = data_table.Rows(j).Item(0)
            cmd.CommandText = "insert into disciplinas_alunos (numero_mec,id_disc) values ('" & Me.txtnumero.Text & "','" & i & "')"
            cmd.ExecuteNonQuery()


        Next
 End Sub

this is the code that is gives me :'( because if i select only one it put it in the table but if i select two or more it's continous to put only one... i don't undestand.

sorry for my english...

Member Avatar for iamthwee

select id_disc from disciplinas where disc_nome = '" & nome & "'", con

That line will fall down when you select more than one item. :D

Member Avatar for iamthwee

Try this...

If CheckedListBox1.CheckedItems.Count <> 0 Then
            ' If so, loop through all checked items and print results.
            Dim x As Integer
            Dim s As String = ""
            For x = 0 To CheckedListBox1.CheckedItems.Count - 1
                s = s & CStr(CheckedListBox1.CheckedItems(x)) & ","
            Next x
            MessageBox.Show(s)
        End If

And wouldn't the following:-

select id_disc from disciplinas where disc_nome = '" & nome & "'"

Need to be like...

SELECT id_disc FROM disciplinas
WHERE disc_nome = 'blah'
     OR disc_nome = 'blahblah'

If you used an arrayList that would be easily doable.

Member Avatar for iamthwee

To illustrate the arrayList ...

Dim crap As New ArrayList()       
        If CheckedListBox1.CheckedItems.Count <> 0 Then
            ' If so, loop through all checked items and print results.
            Dim x As Integer

            For x = 0 To CheckedListBox1.CheckedItems.Count - 1
                crap.Add(CStr(CheckedListBox1.CheckedItems(x)))             
            Next x
        End If

        Dim nome As String = "SELECT id_disc FROM disciplinas WHERE disc_nome ="
        Dim s As String = ""
        Dim i As Integer
        For i = 0 To crap.Count - 1
            If i = 0 Then
                s = s & "'" & crap(i) & "'"
            else
                s = s & " OR disc_nome =" & "'" & crap(i) & "'"
            End If
        Next

        MessageBox.Show(nome & s)

(I don't have my compiler with me so the above is untested)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.