Save CheckedListBox To Database
Hi, I am trying to save checked items from a CheckedListBox to a table in a database, I am using the following code:
Dim conn As New OleDb.OleDbConnection("Provider=sqloledb;" & _
"Data Source=KIM-LAPTOP;" & _
"Initial Catalog=ScrcCardBase;" & _
"User Id=sa;" & _
"Password=sa")
Dim oList As Object
Dim strsql As String = ""
Dim cmd1 As OleDb.OleDbCommand
Try
conn.Open()
For Each oList In Me.clbSites.CheckedItems
strsql = "INSERT INTO tblSiteAllocation (Site) VALUES ('" & oList & "')"
cmd1 = New OleDb.OleDbCommand(strsql, conn)
cmd1.ExecuteNonQuery()
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try
I am getting the following error message:
Operator '&' is not defined for string "INSERT INTO tblSiteAllocation(S" and type 'DataRowView'.
Any ideas as to what may be causing this.
Or does anyone know of an easier way to save checkedlistbox data to a database.
Thanks
Re: Save CheckedListBox To Database
What exactly are you trying to save to the database, just the text of each item or the checked value too?
Re: Save CheckedListBox To Database
Quote:
Originally Posted by
jmcilhinney
What exactly are you trying to save to the database, just the text of each item or the checked value too?
Hi, I am just trying to save the actual text.
Many Thanks
Re: Save CheckedListBox To Database
The fact that the error message mentions DataRowView indicates that you have bound a Datatable to the control in the first place, correct? If so then you should just be using a DataAdapter to save the contents of that table to the database. How did that table get populated in the first place?
Re: Save CheckedListBox To Database
Quote:
Originally Posted by
jmcilhinney
The fact that the error message mentions DataRowView indicates that you have bound a Datatable to the control in the first place, correct? If so then you should just be using a DataAdapter to save the contents of that table to the database. How did that table get populated in the first place?
Hi your correct the checkedlistbox gets populated using a DataAdapter, i will try your suggestion.
Much appreciated