public ProductEntity IsExist(string id) { using (SQLiteConnection sql = new SQLiteConnection(con)) { try { sql.Open(); SQLiteCommand cmd = new SQLiteCommand("select * from Stock where Product_Id = @Id", sql); cmd.Parameters.AddWithValue("@Id", id); SQLiteDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { ProductEntity productEntity = new ProductEntity(); while (reader.Read()) { productEntity.Id = reader.GetInt32(0); productEntity.OldQuantity = reader.GetInt32(2); productEntity.Price = reader.GetInt32(3); } sql.Close(); return(productEntity); } else { sql.Close(); return(new ProductEntity()); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); sql.Close(); return(new ProductEntity()); } } }
public void UpdateStock(int quantity, ProductEntity productEntity, int price, int productID, bool isPriceUpdated) { //int OldQuantity = productEntity.OldQuantity; //int id = productEntity.Id; //bool check = false; //using (SQLiteConnection sql = new SQLiteConnection(con)) //{ // try // { // sql.Open(); // SQLiteCommand cmd = new SQLiteCommand("select Id,Quantity from Stock where Product_Id = @Id", sql); // cmd.Parameters.AddWithValue("@Id", productID); // SQLiteDataReader reader = cmd.ExecuteReader(); // while (reader.Read()) // { // id = reader.GetInt32(0); // OldQuantity = reader.GetInt32(1); // check = true; // } // sql.Close(); // } // catch (Exception ex) // { // MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); // } //} if (AddStockUpdate(quantity, price, productEntity.OldQuantity, productID)) { bool isSuccess = false; using (SQLiteConnection sql = new SQLiteConnection(con)) { try { //if (check) //{ sql.Open(); int finalQuantity = productEntity.OldQuantity + quantity; SQLiteCommand cmd = new SQLiteCommand("update Stock set Quantity=@Quantity where Id=@Id", sql); cmd.Parameters.AddWithValue("@Quantity", finalQuantity); cmd.Parameters.AddWithValue("@Id", productEntity.Id); cmd.ExecuteNonQuery(); MessageBox.Show("Stock Updated Successfully", "Success Message", MessageBoxButtons.OK, MessageBoxIcon.Information); sql.Close(); isSuccess = true; //} } catch (Exception ex) { MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); isSuccess = false; } } if (isPriceUpdated) { if (isSuccess) { product.UpdatePrice(productID, price); } } } }