Results 1 to 5 of 5

Thread: Save CheckedListBox To Database

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    5

    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
    Last edited by Lacool; Apr 14th, 2010 at 09:27 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    5

    Re: Save CheckedListBox To Database

    Quote Originally Posted by jmcilhinney View Post
    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    5

    Re: Save CheckedListBox To Database

    Quote Originally Posted by jmcilhinney View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width