protected void AddOrderToDB(CartItem ci, int order_code) { XhDataContext db = new XhDataContext(); XhOrder order = new XhOrder(); //get current user order.cust_id = db.XhCustomers.Single(c => c.cust_mail.Equals(User.Identity.Name)).cust_id; order.ord_code = order_code; order.ord_date = DateTime.Now; order.ord_delivered = false; order.ord_quantity = ci.Quantity; order.prod_id = ci.ProductId; db.XhOrders.InsertOnSubmit(order); XhProduct product = db.XhProducts.Single(p => p.prod_id == ci.ProductId); //substract <quantity> units from stock for this product //we can't have negative values in stock! int tempstock = product.prod_stock - order.ord_quantity; if (product.prod_stock > 0 && tempstock > 0) { product.prod_stock = tempstock; } //write changes db.SubmitChanges(); }
private object LoadTopSellers(int howMany) { XhDataContext db = new XhDataContext(); //select all prod_ids into new table x, //join x with p in t (a key), //join t with m in order to create //the top 3 of products most bought by ALL clients (not PER) return((from p in db.XhProducts join x in (from o in db.XhOrders join p in db.XhProducts on o.prod_id equals p.prod_id group o by o.prod_id into t select t) on p.prod_id equals x.Key join m in db.XhManufacters on p.man_id equals m.man_id orderby x.Count() descending select new AuxProduct { ImageId = p.img_id, Name = p.prod_name, Price = p.prod_price, Manufacter = m.man_name, Model = p.prod_model, Id = p.prod_id }).Take(howMany)); }
private object LoadCategoriesForLinks() { XhDataContext db = new XhDataContext(); return(from c in db.XhCategories orderby c.cat_name select new { cat_url = "Search.aspx?ucid=" + c.cat_id, cat_name = c.cat_name }); }
private IQueryable <XhProduct> LoadProdDetail(int prodId) { XhDataContext db = new XhDataContext(); return(from p in db.XhProducts where p.prod_id == prodId select p); }
private void InitDdlFilterManufacter() { XhDataContext db = new XhDataContext(); ddlFilterManufacter.DataSource = db.XhManufacters.Select(m => m).OrderBy(m => m.man_name); ddlFilterManufacter.DataBind(); ddlFilterManufacter.Items.Insert(0, new ListItem("All Manufacters", "0")); }
private void InitDdlFilterCategory() { XhDataContext db = new XhDataContext(); ddlFilterCategory.DataSource = db.XhCategories.Select(c => c).OrderBy(c => c.cat_name); ddlFilterCategory.DataBind(); ddlFilterCategory.Items.Insert(0, new ListItem("All Categories", "0")); }
private void InitLvFeatProd() { XhDataContext db = new XhDataContext(); lvFeatProd.DataSource = (from p in db.XhProducts orderby db.Random() select p).Take(6); lvFeatProd.DataBind(); }
/// <summary> /// Returns the next available orderId /// </summary> /// <returns>the next available orderId</returns> internal static int GetNextOrderId() { XhDataContext db = new XhDataContext(); int[] i = (from o in db.XhOrders orderby o.ord_code descending select o.ord_code).Take(1).ToArray(); return(i[0] + 1); }
private void InitBlCatNavBar() { XhDataContext db = new XhDataContext(); var v = db.XhCategories.OrderBy(c => c.cat_name); blCatNavBar.DataSource = LoadCategoriesForLinks(); blCatNavBar.DataBind(); blCatNavBar.Items.Insert(0, new ListItem("All Categories", "Search.aspx?ucid=-1")); }
/// <summary> /// Checks if a given user exists in database /// </summary> /// <returns></returns> internal static bool AuthenticateCustomer(string mail, string password) { XhDataContext db = new XhDataContext(); var v = db.XhCustomers.Where(s => s.cust_mail.Equals(mail) && s.cust_password.Equals(password)); if (v.Count() == 1) { return(true); } return(false); }
internal static IQueryable <XhCategory> SearchCategories(params int[] ids) { var predicate = SearchEngine.False <XhCategory>(); XhDataContext db = new XhDataContext(); foreach (int id in ids) { int temp = id; predicate = predicate.Or(x => x.cat_id.Equals(temp)); } return(db.XhCategories.Where(predicate).OrderBy(x => x.cat_name)); }
/// <summary> /// Returns -1 if prod_id does not exist /// </summary> /// <param name="prod_id"></param> /// <returns></returns> internal static double GetPrice(int prod_id) { XhDataContext db = new XhDataContext(); try { var v = db.XhProducts.Single(p => p.prod_id == prod_id); return(v.prod_price); } catch { return(-1); } }
/// <summary> /// Returns -1 if prod_id does not exist /// </summary> /// <param name="prod_id"></param> /// <returns></returns> internal static int GetStock(int prod_id) { XhDataContext db = new XhDataContext(); try { var v = db.XhProducts.Single(p => p.prod_id == prod_id); return(v.prod_stock); } catch { return(-1); } }
internal static IQueryable <XhProduct> SearchProductsByIdWithoutOne(int[] ids, int notThisOne, byte limitResult) { var predicate = SearchEngine.False <XhProduct>(); XhDataContext db = new XhDataContext(); foreach (int id in ids) { int temp = id; predicate = predicate.Or(x => x.prod_id.Equals(temp) && x.prod_id != notThisOne); } //orderby stock to get more variety! return(db.XhProducts.Where(predicate).OrderByDescending(p => p.prod_stock).Take(limitResult)); }
internal static IQueryable <XhProduct> SearchProducts(params string[] keywords) { var predicate = SearchEngine.False <XhProduct>(); XhDataContext db = new XhDataContext(); foreach (string keyword in keywords) { string temp = keyword; predicate = predicate.Or(p => p.prod_name.Contains(temp) || p.prod_model.Contains(temp) || p.prod_desc.Contains(temp) || p.XhManufacter.man_name.Contains(temp)); } return(db.XhProducts.Where(predicate).OrderBy(p => p.prod_price)); }
internal static IQueryable <XhProduct> FilterProductsByCategoryAndManufacter(string[] keywords, int cid, int mid) { var predicate = SearchEngine.False <XhProduct>(); XhDataContext db = new XhDataContext(); foreach (string keyword in keywords) { string temp = keyword; predicate = predicate.Or(p => (p.prod_name.Contains(temp) || p.prod_model.Contains(temp) || p.prod_desc.Contains(temp) || p.XhManufacter.man_name.Contains(temp)) && (p.cat_id == cid) && (p.man_id == mid)); } return(db.XhProducts.Where(predicate).OrderBy(p => p.prod_price)); }
private void ShowAllProducts() { XhDataContext db = new XhDataContext(); var v = db.XhProducts.Select(p => p).OrderBy(p => p.prod_price); lvProducts.DataSource = v; if (rbFilterPrice.SelectedItem.Value == "d") { lvProducts.DataSource = v.OrderByDescending(p => p.prod_price); } lvProducts.DataBind(); }
protected void Page_Init(object sender, EventArgs e) { Session["mainPage"] = "home"; Session["subPage"] = "homeHome"; XhDataContext db = new XhDataContext(); lvBargain.DataSource = db.XhProducts.Where(p => p.prod_stock > 0).OrderBy(p => p.prod_price).Take(3); lvBargain.DataBind(); lvLatest.DataSource = db.XhProducts.OrderByDescending(p => p.prod_id).Take(3); lvLatest.DataBind(); lvTop.DataSource = LoadTopSellers(3); lvTop.DataBind(); }
private void ShowStats(bool validString, bool validUcid, bool validCid, bool validMid, string searchString, int ucid, int cid, int mid) { XhDataContext db = new XhDataContext(); ltStats.Text = "Found " + howManyResults + " product(s)"; if (validString) { ltStats.Text += " for "" + Server.HtmlEncode(searchString) + """; } if (howManyResults > 0) { if (rbFilterPrice.SelectedItem.Value == "a") { ltStats.Text += ", shown by ascending prices"; } else if (rbFilterPrice.SelectedItem.Value == "d") { ltStats.Text += ", shown by descending prices"; } if (validMid) { ltStats.Text += ", by " + db.XhManufacters.Single(m => m.man_id == mid).man_name; } if (validUcid) { if (ucid == -1) { ltStats.Text += " in all categories"; } else { ltStats.Text += " in category "" + db.XhCategories.Single(c => c.cat_id == ucid).cat_name.ToLower() + """; } } else if (validCid) { ltStats.Text += " in category "" + db.XhCategories.Single(c => c.cat_id == cid).cat_name.ToLower() + """; } } ltStats.Text += "."; }
private void ShowResultBasedOn(int ucid) { XhDataContext db = new XhDataContext(); var v = db.XhProducts.Where(p => p.cat_id == ucid); if (ucid == -1) { v = db.XhProducts.Select(p => p); } lvProducts.DataSource = v.OrderBy(p => p.prod_price); if (rbFilterPrice.SelectedItem.Value == "d") { lvProducts.DataSource = v.OrderByDescending(p => p.prod_price); } ShowOnlyRelevantManufacters(v); this.howManyResults = v.Count(); }
private void ShowResultBasedOnUnsortingCategoryAndManufacter(int ucid, int mid) { XhDataContext db = new XhDataContext(); var v = db.XhProducts.Where(p => p.cat_id == ucid && p.man_id == mid); if (ucid == -1) { v = db.XhProducts.Where(p => p.man_id == mid); } lvProducts.DataSource = v.OrderBy(p => p.prod_price); if (rbFilterPrice.SelectedItem.Value == "d") { lvProducts.DataSource = v.OrderByDescending(p => p.prod_price); } lvProducts.DataBind(); this.howManyResults = v.Count(); }
private IQueryable <XhProduct> LoadAssociatedProducts(int prodId) { XhDataContext db = new XhDataContext(); //Make j.Key (by selecting t) representing customers who have bought this product, //join them with every order ever placed, //select all products that have been bought by these customers IQueryable <int> v = from o in db.XhOrders join j in (from o in db.XhOrders where o.prod_id == prodId group o by o.cust_id into t select t) on o.cust_id equals j.Key select o.prod_id; int[] i = v.ToArray(); //return all products the customers bought except, of course, the product that's being detailed right now and here return(SearchEngine.SearchProductsByIdWithoutOne(i, prodId, 3)); }
private void Page_Load(object sender, System.EventArgs e) { try { string param = Request.QueryString["img"]; string pic_id = param.Substring(0, param.Length - 5); char pic_size = param[pic_id.Length]; Binary pic; XhDataContext db = new XhDataContext(); XhImage image = db.XhImages.Single(i => i.img_id == Int32.Parse(pic_id)); switch (pic_size) { case ('s'): pic = image.img_small; break; case ('m'): pic = image.img_medium; break; case ('l'): pic = image.img_large; break; default: pic = null; break; } Response.ContentType = "image/jpeg"; Response.BinaryWrite(pic.ToArray()); } catch { Response.Redirect("~/Default.aspx"); } }
protected void btnRegister_Click(object sender, EventArgs e) { if (this.IsValid) { XhDataContext db = new XhDataContext(); var v = db.XhCustomers.Where(cust => cust.cust_mail.Equals(tbEmail.Text)); if (v.Count() == 0) { XhCustomer c = new XhCustomer(); //c.cust_country =tbCountry.Text; c.cust_country_code = int.Parse(ddlCountry.SelectedItem.Value); c.cust_mail = tbEmail.Text; c.cust_name = tbName.Text; c.cust_password = tbPassword.Text; c.cust_phone = tbPhone.Text; c.cust_postalcode = int.Parse(tbPostal.Text); c.cust_streetname = tbStreet.Text; c.cust_streetnumber = int.Parse(tbHouse.Text); c.cust_city = tbCity.Text; db.XhCustomers.InsertOnSubmit(c); db.SubmitChanges(); bool authenticated = Customer.AuthenticateCustomer(c.cust_mail, c.cust_password); if (authenticated) { FormsAuthentication.RedirectFromLoginPage(c.cust_mail, false); } Response.Redirect("~/ThankYou.aspx"); } pError.Visible = true; } }