LINQ to SQL multiple checkedlistbox RRS feed

  • Question

  • Hello

    i have windows form application where i need to save the values of a checkedlistbox into a SQL database using LINQ. The only problem is i have no idea how. I tried some code, search on the internet. But notting. I hope you can help me.

                foreach (int indexchecked in checkedListBoxGraad.CheckedIndices)
                    int telgraad = indexchecked + 1;
                    GraadLeerkrachten graden = new GraadLeerkrachten
                        GLkID = Convert.ToInt32(telgraad.ToString())
                Nascholing nascholing = new Nascholing
                    VID = Convert.ToInt32(listBoxVakken.SelectedValue),
                    SoortNSID = Convert.ToInt32(listBoxSoortNascholing.SelectedValue),
                    NsSoortAlID = Convert.ToInt32(listBoxAlgemeen.SelectedValue)

                    // Save the change to the database.

                    // Display the old and new postal code.
                // Detect any concurrency problems.
                catch (ChangeConflictException CCE)
                    // Display the conflict information.
                    // Resolve the conflict.

    I tried to loop through my listitem. But doesn't seem to work, i can't reach the variable "graden" when i"m out of the foreach loop.

    • Moved by SamAgain Tuesday, May 4, 2010 4:18 AM better fit (From:.NET Base Class Library)
    Thursday, April 29, 2010 5:17 PM


  • Hello,

    You could follow the steps below to insert values of a checkedlistbox into database using the code snippet above:
     1. Open Visual Studio and create a Windows Forms application
     2. Drag and drop a CheckedListBox and a Button control from ToolBox to the default Form1
     3. right click the CheckedListBox control and select Properties
     4. named it as checkedListBoxGraad
     5. right click project name and selece Add|New Item..|Visual C# Items|Data|LINQ to SQL Classes
     6. Give it a name and click Add button
        (you may need to follow up the wizzard to create a database connect if you don't have)
     7. Drag and drop tables from Server Explorer (View->Server Explorer)
     8. Add the following code snippet in the button Click event

    TestDBDataContext db = new TestDBDataContext();
    foreach (int index in checkedListBox1.CheckedIndices)
      // this is different according to the database you use.
    Task t = new Task { TaskId = index + 100, Name = "new task", IsComplete = false }; db.Tasks.InsertOnSubmit(t); db.SubmitChanges(); } MessageBox.Show("it's done....");

     Please have a try and let me know the result.

    By the way, here is a linq to sql tutorial, I believe it could help you:
    Also, there are many linq to sql sample here:

    Best regards

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Wednesday, May 5, 2010 8:12 AM