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")

the above code is for inserting multiple records frm listbox to table
but ony the first record gets added

needed help

Recommended Answers

All 2 Replies

it causing your code just insert the selected item.
try to change cstr1 = ListBox1.Items.Item(i)
>> i = 1 --> its not starting with 1 but 0.
First item index is 0 not 1.

For i = 0 To ListBox1.Items.Count - 1
cstr1 = ListBox1.Items.Item(i)
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")

actually i got a little confuse here. you want to insert all item in combobox or multiple selected?

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.