/** * A list of allmagazine in the MagazineClass' getAllMagazineFromDB() function is created. * magazineUserControls is created from the MagazineUserControls * The size of MagazineUserControls is up to the length of allmagazine */ private void populateMagazinesViewInHomePage() { List <MagazineClass> allmagazine = MagazineClass.getAllMagazineFromDB(); MagazineUserControl[] magazineUserControls = new MagazineUserControl[allmagazine.Count]; if (flpHomePage.Controls.Count > 0) { flpHomePage.Controls.Clear(); } int i = 0; foreach (var item in allmagazine) { magazineUserControls[i] = new MagazineUserControl(); magazineUserControls[i].magazineID = item.id; magazineUserControls[i].magazineName = item.name; magazineUserControls[i].magazinePrice = item.price; magazineUserControls[i].magazineIssue = item.issue; magazineUserControls[i].stock = item.stock; magazineUserControls[i].cover_page_picture = item.cover_page_picture; flpHomePage.Controls.Add(magazineUserControls[i]); i++; } }
/**this function read amagazine item in a database * @param string id * @return magazine */ public static MagazineClass getAMagazineFromDB(string id) { DatabaseHelperClass dbHelper = DatabaseHelperClass.Instance; //SINGLETON PATTERN SqlConnection connection = dbHelper.getConnection(); SqlCommand command = new SqlCommand("SELECT * FROM MagazineTable where id=@id", connection); command.Parameters.AddWithValue("@id", Convert.ToInt32(id)); SqlDataReader readMagazine = command.ExecuteReader(); MagazineClass magazine = new MagazineClass(); if (readMagazine != null) { while (readMagazine.Read()) { magazine.id = readMagazine["id"].ToString(); magazine.name = readMagazine["name"].ToString(); magazine.price = Convert.ToDouble(readMagazine["price"]); magazine.stock = Convert.ToInt32(readMagazine["stock"]); magazine.issue = readMagazine["issue"].ToString(); magazine.cover_page_picture = readMagazine["cover_page_picture"].ToString(); } } return(magazine); }
/** @ brief adds the purchased magzine to the shopping cart * create a magzine from MagazineClass * and item added shoppingcartclass */ private void btnMagazineAddToCart_Click(object sender, EventArgs e) { MagazineClass magazine = new MagazineClass(); magazine = MagazineClass.getAMagazineFromDB(magazineID); ShoppingCartClass.addProduct(new ItemToPurchaseClass(magazine, Convert.ToInt32(Math.Round(nudQuantity.Value, 0)))); System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["BookShopForm"]; int quantity = 0; foreach (var item in ShoppingCartClass.itemsToPurchase) { quantity += item.quantity; } ((BookShopForm)f).lblShoppinCartValue.Text = quantity.ToString(); MessageBox.Show("Added to cart.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void btnUpdateMagazine_Click(object sender, EventArgs e) { MagazineClass magazine = new MagazineClass(txtMagazineName.Text, txtMagazineId.Text, Convert.ToDouble(txtMagazinePrice.Text), Convert.ToInt32(txtMagazineStock.Text), txtMagazineIssue.Text, txtMagazineType.Text, txtMagazineImage.Text); //bool affected = dbHelper.updateAProductAtDB(magazine); AdminUserClass adminOps = AdminUserClass.Instance; bool affected = adminOps.updateProduct(magazine); if (!affected) { MessageBox.Show("Error not successful", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("Magazine added successfully", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information); clearMagazineControls(); } }
/** * magazineList created fromMagazineClass * this function reads magazine items in database * and add in magazinelist * @return magazineList */ public static List <MagazineClass> getAllMagazineFromDB() { List <MagazineClass> magazineList = new List <MagazineClass>(); DatabaseHelperClass dbHelper = DatabaseHelperClass.Instance; //SINGLETON PATTERN SqlConnection connection = dbHelper.getConnection(); SqlCommand command = new SqlCommand("SELECT * FROM MagazineTable", connection); SqlDataReader readMagazine = command.ExecuteReader(); if (readMagazine != null) { while (readMagazine.Read()) { MagazineClass magazine = new MagazineClass(); magazine.id = readMagazine["id"].ToString(); magazine.name = readMagazine["name"].ToString(); magazine.price = Convert.ToDouble(readMagazine["price"]); magazine.stock = Convert.ToInt32(readMagazine["stock"]); magazine.issue = readMagazine["issue"].ToString(); magazine.cover_page_picture = readMagazine["cover_page_picture"].ToString(); magazineList.Add(magazine); } } return(magazineList); }