Question inserting multiple records frm listbox

svibuk

Active member
Joined
Jul 15, 2008
Messages
31
Programming Experience
1-3
VB.NET:
For i = 1 To ListBox1.Items.Count - 1
                cstr1 = ListBox1.Text
                strqry1 = "insert into trans(pid) values('" & cstr1 & "')"
                MessageBox.Show(strqry1)
                If ListBox1.GetSelected(i) = True Then
                    cmd.CommandText = strqry1
                    cmd.ExecuteNonQuery()
                End If
            Next
            MessageBox.Show("Records added")


uisng the above code fr inserting multiple data from listbox to dtatabse
but only first record gets inserted

help appreciated
 
Hello.

What do you wanna do? Only inserting the selected entries? If yes try this instead:

VB.NET:
        For Each entry As Object In Me.lstBx_choice.SelectedItems
            cmd.CommandText = "insert into trans(pid) values('" & entry.ToString & "')"
            cmd.ExecuteNonQuery()
        Next

Bobby
 
Back
Top