none
unload list control values into linq to sql to a database RRS feed

  • Question

  • In a C#.net 2010 application, I would like to take
    values entered into individual checkbox and place each value of the
    checkboxes into separate rows in a sql server 2008 r2 table. I would
    also like to use linq to sql for this to occur. Since I am new to
    working with linq, I am wondering how I could incorporate linq
    into one of the options listed below. Would I need to unload all the
    values from the checkboxes into a arraylist. Can you tell me
    how to complete this task or point me to utls I can use as a
    reference?

    Here are some of my code examples:

    option 1:

    foreach (ListItem item in
    ((CheckBoxList)Form.FindControl("cblActivities")).Items)
                {
                    if (item.Selected)
                    {
                       string Activities += item.Value; 
                       
                    }
                }

    option 2

    IEnumerable<int> allChecked = chkBoxList.Items.Cast<ListItem>()
                                  .Where(i => i.Selected)
                                  .Select(i => int.Parse(i.Value));

    Saturday, October 29, 2011 12:29 AM

Answers

  • yes, activitytable is a table I created in sql just to use as an example.  It then got added as a part of the datacontext.  So, your datacontext should have a similar table which my understanding it's the one you want to hold the checkbox items, one row for each checked item in the checkboxlist.  
    Tom Overton
    Monday, October 31, 2011 5:01 PM

All replies

  • Hi Wendy,

    I'm not sure how your table is structured, but if you just want to insert a record for each item that was checked (so for example a varchar column of the table column would be the value of the checkbox description) you could do something like this:

     protected void Button1_Click(object sender, EventArgs e)
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
    
                    var results = cblActivities.Items.Cast<ListItem>().Where(i => i.Selected == true);
    
                    foreach (ListItem item in results)
                    {
                       ActivityTable d = new ActivityTable();
    
                        d.Description = item.Value;
    
                        db.ActivityTables.InsertOnSubmit(d);
    
                    }
    
                    db.SubmitChanges();
                }
                
            }
    

     


    Tom Overton
    Saturday, October 29, 2011 5:25 PM
  • I want to mention the following in regards to your answer above:

    1. The activity table you mentioned above is basically like the table I am working with. The only difference is my table has one extra column that is the foreign key to the master table. So the extra column in my table is called main_id and it references to the key in the Master table.

    2. The following statement probably works, " var results = cblActivities.Items.Cast<ListItem>().Where(i => i.Selected == true);"

       I see 'results' will contain the records where the checkbox were selected. I do understand how the for loop below relates exactly the  var results statement.

    Basically I do not understand the lamda expressions very well. If I could 'step through' code, I know I would understand the code better.

    The expressions I listed in my original website listing, are ones I found online.

    Thus is there a way to 'step through' (debug each line of code) so I can understand it better? If so can you tell me and/or point me to a reference on how to step through the linq and/or lamda expressions?

    I am asking that question since I understand code better if I can 'debug' step through code regardless of what language it is written in.

     

    Saturday, October 29, 2011 7:47 PM
  • Hi Wendy,

    Yes I know it can be confusing at first to understand what a lambda expression is doing.  The benefits of it (meaning writing less code) also have drawbacks because there isn't code to "step into" and see what's going on.

    So, what I did was I broke it apart to  help show what it's doing if we did it the long way.  Meaning, what it does if we didn't use a lambda but instead used a foreach loop of the entire list of checkbox items.  We loop through the whole list and figure out which ListItem is checked.  The lambda expression did this in only half a line of code: .Where(i => i.Selected == true); .

    So in the lambda expression, we made up a variable name (i) to represent each item in the enumeration, and on the right side we made a filter condition that will happen for each item in the enumeration.

    So here it is doing it the "long way" so you can see what's going on inside the .Where(...) lambda expression part:

     

     protected void Button1_Click(object sender, EventArgs e)
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                                   
    
                    List<ListItem> results = new List<ListItem>();              
    
    
                    //all this is doing is taking the ListItemCollection that is part of the list box  and converting it to a IEnumerable<ListItem>() which we then assign to a List
                    var allresults = cblActivities.Items.Cast<ListItem>();   
    
                    //the foreach below is what our .Where clause was doing
                    foreach (ListItem item in allresults)
                    {
                        bool i = item.Selected;
                        if (i == true)
                            results.Add(item);
    
                    }
                    
                    foreach (ListItem item in results)
                    {
                       ActivityTable d = new ActivityTable();
    
                        d.Description = item.Value;
    
                        db.ActivityTables.InsertOnSubmit(d);
    
                    }
    
                    db.SubmitChanges();
                }
                
            }
    


     


    Tom Overton

    Saturday, October 29, 2011 8:19 PM
  • I am trying to get the following code to work, but the compiler does not know what ActivityTable d = new ActivityTable();

    is in the following statment. Thus can you tell me what I am missing? Do I need a using statement?

     protected void Button1_Click(object sender, EventArgs e)
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {

                    var results = cblActivities.Items.Cast<ListItem>().Where(i => i.Selected == true);

                    foreach (ListItem item in results)
                    {
                       ActivityTable d = new ActivityTable();

                        d.Description = item.Value;

                        db.ActivityTables.InsertOnSubmit(d);

                    }

                    db.SubmitChanges();
                }
               
            }

    Monday, October 31, 2011 4:26 PM
  • Wendy,

    You'll have to substitute your own table names, those were just the ones I used as an example.  You'll have to look in the designer and also use intellisense to see what the correct table and column property names are for your environment.

    Also, You will probably have a different datacontext name (mine was the default name DataClasses1DataContext).


    Tom Overton
    Monday, October 31, 2011 4:35 PM
  • Is your table 'ActivityTable d =new ActivityTable();', the name of a table in the datacontext obejct? I was thinking the table was a data table in a ado.net dataset object for some reason.

    Monday, October 31, 2011 4:53 PM
  • yes, activitytable is a table I created in sql just to use as an example.  It then got added as a part of the datacontext.  So, your datacontext should have a similar table which my understanding it's the one you want to hold the checkbox items, one row for each checked item in the checkboxlist.  
    Tom Overton
    Monday, October 31, 2011 5:01 PM