?
Solved

Updating items from a checkboxlist using LINQ

Posted on 2010-01-11
5
Medium Priority
?
472 Views
Last Modified: 2012-05-08
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
Comment
Question by:ApexCo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26294051
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
 
LVL 8

Author Comment

by:ApexCo
ID: 26294092
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26294315
How are the list items added to the ListBox?  Is it text, and primary key value?  What does unchecking an item mean?
0
 
LVL 8

Author Comment

by:ApexCo
ID: 26294362
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 26298397
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question