none
How to add the items from the checkedlistbox into the database? RRS feed

  • Question

  • How do we add the items from the checklistbox into the database? I'm using visual basic 2008 express edition via linq to sql method. 
    I was trying to save the items from the checklistbox into my database but I encountered such an error. 
    I was only able to add one record instead of all the records which are being displayed from the checklistbox. 
    What is happening in my end is all the table columns in my database are being supplied with only one record which is the first item on the checklistbox. 
    It only adds the bar code on all the table columns and the rest of the items in the checklistbox did not come in. Here is the code I used: Please help... 

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

            Dim rep As New Table3
            
            rep.Bar_Code = CheckedListBox1.Items.Item(rep.Bar_Code)
            rep.Movies_Rented = CheckedListBox1.Items.Item(rep.Movies_Rented)
            rep.No__of_Copies_Rented = CheckedListBox1.Items.Item(rep.No__of_Copies_Rented)
            rep.Date_Rented = CheckedListBox1.Items.Item(rep.Date_Rented)
            rep.Rental_Days = CheckedListBox1.Items.Item(rep.Rental_Days)

            db.Table3s.InsertOnSubmit(rep)
            db.SubmitChanges()
            Form8.Table3TableAdapter.Fill(Form8.Rentedrec.Table3)
     
    End Sub
    Monday, January 17, 2011 2:11 PM

Answers

  • yes you can loop for the checkboxlist item like this too

     Dim rep As New Table3

    List<Table3> table3List = from checkItem in Checkboxlist1.Items.Cast<CheckboxItem>()

                                        select new Table3() {  

                                                       Bar_Code = checkItem(rep.Bar_Code),
                                                       Movies_Rented = checkItem(rep.Movies_Rented),
                                                       No__of_Copies_Rented = checkItem(rep.No__of_Copies_Rented),
                                                       Date_Rented = checkItem(rep.Date_Rented),
                                                       Rental_Days = checkItem(rep.Rental_Days)
                                         } 

    db.Table3s.InsertOnSubmit(table3List )

    as InsertOnSubmit Accepts both IEnumerable and single objects.

    db.SubmitChanges()
    Form8.Table3TableAdapter.Fill(Form8.Rentedrec.Table3)

     

     


    Nikx Gupta MCP, MCTS
    Friday, January 21, 2011 9:23 AM

All replies

  • Hi timmack,

    Welcome to MSDN forums.

    According to your code and description, you use LINQ to SQL to insert Table3 object? but I saw TableAdapter.Fill(), I am very confused.

        rep.Bar_Code = CheckedListBox1.Items.Item(rep.Bar_Code)
        rep.Movies_Rented = CheckedListBox1.Items.Item(rep.Movies_Rented)
        rep.No__of_Copies_Rented = CheckedListBox1.Items.Item(rep.No__of_Copies_Rented)
        rep.Date_Rented = CheckedListBox1.Items.Item(rep.Date_Rented)
        rep.Rental_Days = CheckedListBox1.Items.Item(rep.Rental_Days)
    

    Here is right, you may try db.Table3s.Add(rep);

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 18, 2011 7:27 AM
    Moderator
  • Hi Alan!

    Thank you for the reply. I tried to use your code 'db.Table3s.Add(rep)' via linq to sql method but it is not recognized by vb 2008 express ed.

    I can't find that line, I can only find this line 'db.Table3s.InsertOnSubmit(rep)'. This line of code

    'Form8.Table3TableAdapter.Fill(Form8.Rentedrec.Table3)' that you are confused with doesn't matter. Its optional, you can remove it from

    there. It only serves as a real-time update everytime you look at your database in form8.

    The only thing I'm confused with is why all the table columns in my database are filled with only one value, which is the value of the bar code(rep.Bar_Code) in the checkedlistbox when you execute the command button. I hope you can help me with this.

    Tuesday, January 18, 2011 11:06 AM
  • Do I need to take a loop around the checkedlistbox? If so,how do I do it? Please help. I need to get all the items being displayed in the checkedlistbox and saves it into my database?
    Wednesday, January 19, 2011 6:17 AM
  • yes you can loop for the checkboxlist item like this too

     Dim rep As New Table3

    List<Table3> table3List = from checkItem in Checkboxlist1.Items.Cast<CheckboxItem>()

                                        select new Table3() {  

                                                       Bar_Code = checkItem(rep.Bar_Code),
                                                       Movies_Rented = checkItem(rep.Movies_Rented),
                                                       No__of_Copies_Rented = checkItem(rep.No__of_Copies_Rented),
                                                       Date_Rented = checkItem(rep.Date_Rented),
                                                       Rental_Days = checkItem(rep.Rental_Days)
                                         } 

    db.Table3s.InsertOnSubmit(table3List )

    as InsertOnSubmit Accepts both IEnumerable and single objects.

    db.SubmitChanges()
    Form8.Table3TableAdapter.Fill(Form8.Rentedrec.Table3)

     

     


    Nikx Gupta MCP, MCTS
    Friday, January 21, 2011 9:23 AM