private void Save_Click(object sender, EventArgs e) { //Create the orders try { Connect connectObj = new Connect(); con = connectObj.connect(); SqlCommand cmd = new SqlCommand("Insert into MYORDER (ORD_ID,CID,DATE,AMOUNT) values(@oid,@cid,@date,@amount);", con); cmd.Parameters.AddWithValue("@oid", OrderID.Text); cmd.Parameters.AddWithValue("@cid", cid); cmd.Parameters.AddWithValue("@date", date.Text); cmd.Parameters.AddWithValue("@amount", Total_Amt.Text); int i = cmd.ExecuteNonQuery(); //If count is equal to 1, than show frmMain form if (i != 0) { MessageBox.Show("Order Insertion Successful!", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Order Insertion Failed", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error); } con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (con != null) { con.Close(); } } //Insert into orderDetails try { int j; Connect connectObj = new Connect(); con = connectObj.connect(); for (int i = 0; i < transactionDT.Rows.Count; i++) { SqlCommand cmd = new SqlCommand("Insert into ORDER_DETAILS (ord_id,pid,quantity) values(@oid,@pid,@quantity);", con); cmd.Parameters.AddWithValue("@oid", OrderID.Text); cmd.Parameters.AddWithValue("@pid", transactionDT.Rows[i]["Product ID"].ToString()); cmd.Parameters.AddWithValue("@quantity", transactionDT.Rows[i]["Quantity"].ToString()); j = cmd.ExecuteNonQuery(); } con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (con != null) { con.Close(); } } //Update stock table after creating order try { int j; Connect connectObj = new Connect(); con = connectObj.connect(); for (int i = 0; i < transactionDT.Rows.Count; i++) { SqlCommand cmd = new SqlCommand("UPDATE STOCK SET QUANTITY = QUANTITY - @quantity WHERE PID = @pid", con); cmd.Parameters.AddWithValue("@pid", transactionDT.Rows[i]["Product ID"].ToString()); cmd.Parameters.AddWithValue("@quantity", transactionDT.Rows[i]["Quantity"].ToString()); j = cmd.ExecuteNonQuery(); } con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (con != null) { con.Close(); } } //Clear all the fields. Cust_search.Clear(); CustName.Clear(); PhoneNumber.Clear(); Cust_address.Clear(); custEmail.Clear(); Product_search.Clear(); Product_Name.Clear(); ProductID.Clear(); Total_Amt.Text = "0"; SubTotal.Clear(); Discount.Text = "0"; GST.Clear(); PaidAmount.Clear(); ReturnAmount.Clear(); Quantity.Text = "0"; Amount.Text = "0"; AddedProducts.DataSource = null; }
private void Add_Click(object sender, EventArgs e) { String PNAME = Product_Name.Text; String PID = ProductID.Text; if (Quantity.Text == "" || Product_Name.Text == "" || ProductID.Text == "") { MessageBox.Show("Enter the Quantity!!", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { Connect connectObj = new Connect(); con = connectObj.connect(); SqlCommand cmd = new SqlCommand("Select * from STOCK where PID =@pid and QUANTITY >= @quantity", con); cmd.Parameters.AddWithValue("@pid", ProductID.Text); cmd.Parameters.AddWithValue("@quantity", Quantity.Text); SqlDataAdapter adapt = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapt.Fill(ds); con.Close(); int count = ds.Tables[0].Rows.Count; //If count is equal to 1, than show frmMain form if (count != 1) { MessageBox.Show("Check stock!!!!", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (con != null) { con.Close(); } } decimal quantity = decimal.Parse(Quantity.Text); decimal amount = decimal.Parse(Amount.Text); decimal total = amount * quantity; decimal subtotal = decimal.Parse(SubTotal.Text); subtotal += total; if (PNAME == "") { MessageBox.Show("select a product and try again", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { transactionDT.Rows.Add(PNAME, PID, quantity, amount, total); AddedProducts.DataSource = transactionDT; SubTotal.Text = subtotal.ToString(); if (ProductID.Text == "" || OrderID.Text == "") { MessageBox.Show("Please provide all the details", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Product_Name.Clear(); ProductID.Clear(); Quantity.Text = "0"; Amount.Text = "0"; } }