public static List<Product> ReadProductsFromDataBase() { var listOfProducts = new List<Product>(); using (var reader = new StreamReader(FileOperation.FilePath)) { var CurrentProduct = reader.ReadLine(); while (CurrentProduct != null) { var currentProduct = CurrentProduct.Split('|'); var product = new Product( DateTime.Parse(currentProduct[0].Trim()), currentProduct[1].Trim(), double.Parse(currentProduct[2].Trim())); listOfProducts.Add(product); CurrentProduct = reader.ReadLine(); } } return listOfProducts; }
public static bool AddProductToDataBase(Product productToAdd) { try { using (var writer = new StreamWriter(FileOperation.FilePath, true)) { writer.WriteLine(productToAdd); } return true; } catch (Exception) { return false; } }
protected void importbutton_Click(object sender, EventArgs e) { var productToAdd = new Product( DateTime.Parse(this.DateTxtBox.Text), this.productxt.Text, double.Parse(this.pricetxtbox.Text)); if (FileOperation.AddProductToDataBase(productToAdd)) { this.msglabel.Text = "Successfully added item"; this.DateTxtBox.Text = string.Empty; this.productxt.Text = string.Empty; this.pricetxtbox.Text = string.Empty; } else { this.msglabel.ForeColor = System.Drawing.Color.Red; this.msglabel.Text = "An error occured while adding the product"; } }