back up codes

always have a backup of your code

Archive for the tag “listbox”

select listbox item if it exist in database

The following code would select/deselect a listbox item, depending if the item is found in the database or not.

On the DataBound event of the listbox, insert the following code:


For i = 0 To myListBox.Items.Count - 1
myitem = myListBox.Items(i).Value
command = New Data.SqlClient.SqlCommand("SELECT * FROM MY_TABLE WHERE MY_PK = '" & myitem & "'", myconnection)
reader = command.ExecuteReader()
If reader.Read() Then
myListBox.Items(i).Selected = True
Else
myListBox.Items(i).Selected = False
End If
reader.Close()
Next

insert to database if listbox item is selected

The following code will insert the data selected on a listbox.


Dim i As Integer
For i = 0 To myListbox.Items.Count - 1
If myListbox.Items(i).Selected Then
(insert to database)
End If
Next

For SQL insert command, go to this link

Post Navigation

Follow

Get every new post delivered to your Inbox.