using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;


public partial class _Default : System.Web.UI.Page
{
    DataClassesDataContext context = new DataClassesDataContext();

    public IQueryable<Product> GetProducts([Control("RadComboBoxCategories")] int? categoryValue)
    {
        if (categoryValue.HasValue)
        {
            return from p in context.Products
                   where p.CategoryID == categoryValue
                   select p;
        }

        return from p in context.Products
               select p;
    }

    public void UpdateProduct(int ProductID)
    {
        Product updatedProduct = context.Products.Where(p => p.ProductID == ProductID).First();
        TryUpdateModel(updatedProduct);

        if (ModelState.IsValid)
        {
            context.SubmitChanges();
        }
    }

    public void InsertProduct(Product p)
    {
        if (ModelState.IsValid)
        {
            context.Products.InsertOnSubmit(p);
            context.SubmitChanges();
        }
    }

    public void DeleteProduct(int ProductID)
    {
        Product deletedProduct = context.Products.Where(p => p.ProductID == ProductID).First();
        context.Products.DeleteOnSubmit(deletedProduct);
        context.SubmitChanges();
    }
}