private void btnDelete_Click(object sender, RoutedEventArgs e) { var itemsjson = File.ReadAllText(@"Products.json"); try { var jObject = JObject.Parse(itemsjson); JArray itemsArr = (JArray)jObject["Products"]; Button button = sender as Button; Classes.Items itemId = button.CommandParameter as Classes.Items; int iId = itemId.Id; var ItemToDelete = itemsArr.FirstOrDefault(obj => obj["Id"].Value <int>() == iId); MessageBox.Show("Are you sure you want to delete this product?"); itemsArr.Remove(ItemToDelete); string output = Newtonsoft.Json.JsonConvert.SerializeObject(jObject, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(@"Products.json", output); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { MessageBox.Show("Product Deleted!"); GetItemsList(); } }
private void btnSave_Click(object sender, RoutedEventArgs e) { try { Classes.Items i = new Classes.Items(); i.Id = Convert.ToInt32(txtId.Text); i.Name = txtName.Text; i.Price = Convert.ToDecimal(txtPrice.Text); i.Upc = txtupc.Text; i.Quantity = Convert.ToInt32(txtQuantity.Text); i.Color = cmbColor.Text; i.Category = cmbCategory.Text; i.Image = txtfilepath.Text; i.Description = txtDescription.Text; if (inStock.IsChecked == true) { i.IsStock = "In Stock"; } if (outStock.IsChecked == true) { i.IsStock = "Out of Stock"; } i.Image = txtfilepath.Text; var newItem = "{ 'Id': '" + i.Id + "', 'Name': '" + i.Name + "', 'Price': '" + i.Price + "', 'Upc': '" + i.Upc + "', 'Quantity': '" + i.Quantity + "', 'Color': '" + i.Color + "', 'Category': '" + i.Category + "', 'IsStock': '" + i.IsStock + "', 'Image': '" + i.Image + "', 'Description': '" + i.Description + "'}"; var itemJson = File.ReadAllText(@"Products.json"); var jsonObject = JObject.Parse(itemJson); var itemArr = jsonObject.GetValue("Products") as JArray; var customer = JObject.Parse(newItem); itemArr.Add(customer); jsonObject["Products"] = itemArr; string jsonResult = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); File.WriteAllText(@"Products.json", jsonResult); } catch (Exception ex) { MessageBox.Show("Error occured : " + ex.Message.ToString()); } finally { MessageBox.Show("New Product Added!"); txtId.Clear(); txtName.Clear(); txtPrice.Clear(); txtQuantity.Clear(); txtupc.Clear(); itemPic.Source = null; txtDescription.Clear(); cmbCategory.Text = "--Select Category--"; cmbColor.Text = "--Select Color--"; } }
private void btnView_Click(object sender, RoutedEventArgs e) { Button b = sender as Button; Classes.Items items = b.CommandParameter as Classes.Items; id = items.Id; name = items.Name; price = items.Price; upc = items.Upc; quantity = items.Quantity; color = items.Color; category = items.Category; isstock = items.IsStock; image = items.Image; desc = items.Description; SingleProduct sp = new SingleProduct(); sp.Show(); this.Close(); }