• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

Updating items from a checkboxlist using LINQ

I'm not having any luck figuring out how to make this work.

I'm using the following code to insert the items from the checkboxlist into the database, each record on its own row. My dilemma is how to save the changes back to the database if a user unchecks a checkbox in the list or checks one that wasn't previously checked.
for (int i = 0; i < listAppliances.Items.Count; i++)
                    {
                        if (listAppliances.Items[i].Selected)
                        {
                            Appliance a = new Appliance();
                            a.applianceType = int.Parse(listAppliances.Items[i].Value);
                            a.propertyId = p.propertyId;
                            
                            //Save object to database
                            Repository.Insert<Appliance>(a);

                            //Reset object to nothing
                            a = null;                            
                        }
                    }

Open in new window

0
ApexCo
Asked:
ApexCo
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
I don't quite understand the context for the question.

What is "Repository", and what is this line supposed to do?

      Repository.Insert<Appliance>(a);
0
 
ApexCoAuthor Commented:
That line is this, I've put all of my data access in a separate project.

I'm looking for the most efficient way to update the items in a checkboxlist.

If I return the data and something is checked, and a user unchecks it to edit the record, will the datacontext know this has changed and delete that from the DB, or do I need to update that myself? So if I need to do it myself (I'm assuming) what is the best practice to make the necessary changes?


public static void Insert<T>(T item) where T : class
        {

            try
            {
                // get the appropriate table by type and then
                // insert the passed in object and submit
                // that change to the data context
                using (DataClassesDataContext context = new DataClassesDataContext())
                {
                    // get the table by the type passed in
                    var table = context.GetTable<T>();

                    // pass in the object with insert on submit
                    // and then submit changes
                    table.InsertOnSubmit(item);
                    context.SubmitChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

Open in new window

0
 
Bob LearnedCommented:
How are the list items added to the ListBox?  Is it text, and primary key value?  What does unchecking an item mean?
0
 
ApexCoAuthor Commented:
Text and primary key value, yes.

Unchecking means something like the user would enter a property, and add say a refrigerator to that property. Something happens and they need to remove it. So when they go to edit the property, the checkbox for refrigerator would be checked, and if they uncheck it I need to update that record in the DB, and in this case it would be deleting it since the appliances are stored in their own table.

ApplianceId
PropertyId
ApplianceTypeId

0
 
Bob LearnedCommented:
If that is the case, then I would use the primary key to search in the table for that value, get the Appliance instance, and then if the CheckBox is unchecked, call DataContext.Appliances.DeleteOnSubmit(appliance).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now