locked
Unable to update checkboxes on Junction table RRS feed

  • Question

  • User1122679459 posted

    I'm trying to update some checkbox values on a junction table but I'm not getting the result I need.

    I have three dropdowns with check boxes (CategoryID, AgeID and GenderID), I'm able to select multiple form each. If I check or uncheck one and run my update nothing happens.

    For example, If I uncheck CategoryID, it should clear that entire row where the CategoryID is "2"..

    Here is my update code:

    var ProductID = UrlData[0];
    var CategoryID = Request["Category"]; var GenderID = Request["Gender"]; var AgeID = Request["Age"];

    var db = Database.Open("Data"); var sqlProduct = "SELECT * FROM Products WHERE ProductID = @0"; var product = db.QuerySingle(sqlProduct, ProductID); var sqlProductDetail = "SELECT * FROM ProductDetail WHERE ProductID = @0"; var productDetail = db.QuerySingle(sqlProductDetail, ProductID); var brandID = db.QueryValue("SELECT BrandID FROM Junction WHERE ProductID = @0", ProductID); var categories = db.Query("SELECT CategoryID, CategoryName FROM Categories"); var categoriesChecked = db.Query("SELECT CategoryID FROM Junction WHERE ProductID = @0", ProductID).Select(c => (int)c.CategoryID); var age = db.Query("SELECT AgeID, AgeName FROM Age"); var ageChecked = db.Query("SELECT AgeID FROM Junction WHERE ProductID = @0", ProductID).Select(a => (int)a.AgeID); var gender = db.Query("SELECT GenderID, GenderName FROM Gender"); var genderChecked = db.Query("SELECT GenderID FROM Junction WHERE ProductID = @0", ProductID).Select(g => (int)g.GenderID); var Category = CategoryID.Split(','); var Gender = GenderID.Split(','); var Age = AgeID.Split(','); foreach (string crow in Category) { foreach (string arow in Age) { foreach (string grow in Gender) { //get the IDs CategoryID = crow.Trim(); //get the IDs AgeID = arow.Trim(); //get the IDs GenderID = grow.Trim(); // Update var sqlUpdateCategories = "UPDATE Junction SET CategoryID = @0, AgeID = @1, GenderID = @2, BrandID = @3 WHERE ProductID = @4 AND CategoryID = @0 AND AgeID = @1 AND GenderID = @2 AND BrandID = @3"; db.Execute(sqlUpdateCategories, CategoryID, AgeID, GenderID, brandID, ProductID); } } }

    Here is my html:

     <ul class="dropdown-menu" role="menu">
                            @foreach (var item in categories.Select(i => (int)i.CategoryID))
                            {
                                <li>
                                    <div class="checkbox">
                                        <label>
                                            <input type="checkbox" name="Category" value="@item" checked="@(categoriesChecked.Contains(item))" /> @categories.First(c => c.CategoryID == item).CategoryName
                                        </label>
                                    </div>
                                </li>
                            }
                        </ul>
    
    
    <ul class="dropdown-menu" role="menu">
                            @foreach (var item in age.Select(i => (int)i.AgeID))
                            {
                                <li>
                                    <div class="checkbox">
                                        <label>
                                            <input type="checkbox" name="Age" value="@item" checked="@(ageChecked.Contains(item))" /> @age.First(a => a.AgeID == item).AgeName
                                        </label>
                                    </div>
                                </li>
                            }
                        </ul>
    
    
    <ul class="dropdown-menu" role="menu">
                            @foreach (var item in gender.Select(i => (int)i.GenderID))
                            {
                                <li>
                                    <div class="checkbox">
                                        <label>
                                            <input type="checkbox" name="Gender" value="@item" checked="@(genderChecked.Contains(item))" /> @gender.First(g => g.GenderID == item).GenderName
                                        </label>
                                    </div>
                                </li>
                            }
                        </ul>

    Wednesday, May 27, 2015 12:06 PM

Answers

  • User1122679459 posted

    I never receive any comments on this one but I figured it out.

    I just ran a delete of all of the categories that have the product id X, then ran the update. (voila!)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2015 9:07 AM