Beispiel #1
0
        public bool InsertTransactionDetail(transcationDetailsBLL td)
        {
            //Create a boolean value and set its default value to false
            bool isSuccess = false;

            //Create a database connection here
            SqlConnection conn = new SqlConnection(myconnstrng);

            try
            {
                //Sql Query to Insert Transaction Details
                string sql = "INSERT INTO tbl_transaction_detail (product_id, rate, qty, total, dea_cust_id, added_date, added_by) VALUES (@product_id, @rate, @qty, @total, @dea_cust_id, @added_date, @added_by)";

                //Passing the value to the SQL Query
                SqlCommand cmd = new SqlCommand(sql, conn);

                //Passing the Values using cmd
                cmd.Parameters.AddWithValue("@product_id", td.product_id);
                cmd.Parameters.AddWithValue("@rate", td.rate);
                cmd.Parameters.AddWithValue("@qty", td.qty);
                cmd.Parameters.AddWithValue("total", td.total);
                cmd.Parameters.AddWithValue("dea_cust_id", td.dea_cust_id);
                cmd.Parameters.AddWithValue("@added_date", td.added_date);
                cmd.Parameters.AddWithValue("@added_by", td.added_by);

                //Open Sql Database Connnection
                conn.Open();

                //declare the int variable and execute the Query
                int rows = cmd.ExecuteNonQuery();

                if (rows > 0)
                {
                    //Query Executed Sucessfully
                    isSuccess = true;
                }
                else
                {
                    //Failed to Execute Query
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //Close Databse Connection
                conn.Close();
            }

            return(isSuccess);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            //Get the Values or details from the PurchaseForm first
            transactionsBLL transaction = new transactionsBLL();

            transaction.type = lblTop.Text;

            //Get the ID of Dealer or Customer Here
            //Lets get name of the dealer or customer first
            string deaCustName = txtName.Text;

            DeaCustBLL dc = dcDAL.GetDeaCustIDFromName(deaCustName); //***********************

            transaction.dea_cust_id      = dc.id;                    //***********************************
            transaction.grandTotal       = Math.Round(decimal.Parse(txtGrandTotal.Text), 2);
            transaction.transaction_date = DateTime.Now;
            transaction.tax      = decimal.Parse(txtVat.Text);
            transaction.discount = decimal.Parse(txtDiscount.Text);

            //Get the Username of the Logged in User
            string  username = frmLogin.loggedIn;
            userBLL u        = uDAL.GetIDFromUsername(username);

            transaction.added_by           = u.id;
            transaction.transactionDetails = transactionDT;

            //Lets Create a Boolean Variable and Set it's value to False
            bool success = false;

            //Actual code to Insert Transaction and Transaction Details
            using (TransactionScope scope = new TransactionScope())
            {
                int tranasctionID = -1;

                //Create a boolean Value and Insert Transaction
                bool w = tDAL.Insert_Transction(transaction, out tranasctionID);

                //use for  loop to Insert Transaction Details
                for (int i = 0; i < transactionDT.Rows.Count; i++)
                {
                    //Get all the details of the product
                    transcationDetailsBLL transcationDetails = new transcationDetailsBLL();

                    //Get the product Name and convert it to id
                    string ProductName = transactionDT.Rows[i][0].ToString(); // Here was Errror before<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

                    productsBLL p = pDAL.GetDeaProductIDFromName(ProductName);
                    transcationDetails.product_id  = p.id;
                    transcationDetails.rate        = decimal.Parse(transactionDT.Rows[i][1].ToString());
                    transcationDetails.qty         = decimal.Parse(transactionDT.Rows[i][2].ToString());
                    transcationDetails.total       = Math.Round(decimal.Parse(transactionDT.Rows[i][3].ToString()), 2);
                    transcationDetails.dea_cust_id = dc.id;
                    transcationDetails.added_date  = DateTime.Now;
                    transcationDetails.added_by    = u.id;

                    /*------------------------------------------------------------------------------------------------------------------
                     * ------------ PRODUCT INCREASE OR DECREASE OR UPDATE ---------------------------------------------------------------
                     * -------------------------------------------------------------------------------------------------------------------*/


                    //Here Increase or Decrease Product Quantity based on Purchase or Sales
                    string transactionType = lblTop.Text;

                    //Lets check whether we are on Purchase or Sales
                    bool x = false;
                    if (transactionType == "Purchase")
                    {
                        //Increase the product
                        x = pDAL.IncreaseProduct(transcationDetails.product_id, transcationDetails.qty);
                    }
                    else if (transactionType == "Sales")
                    {
                        //Decrease the Product Quantity
                        x = pDAL.DecreaseProduct(transcationDetails.product_id, transcationDetails.qty);
                    }

                    /*---------------------------------------------------------------------------------------------------------------------------
                     * ------------------------PRODUCT INCREASE OR DECREASE OR UPDATE--------------------------------------------------------------
                     * ----------------------------------------------------------------------------------------------------------------------------*/

                    //Insert Transaction Details inside the database
                    bool y = tdDAL.InsertTransactionDetail(transcationDetails);
                    success = w && x && y;
                }

                if (success == true)
                {
                    //Transaction Complete
                    scope.Complete();
                    MessageBox.Show("Transaction Completed Sucessfully");

                    //Clear Data Grid View and all of the Textboxes
                    dgvAddedProoducts.DataSource = null;
                    dgvAddedProoducts.Rows.Clear();

                    txtSearch.Text        = "";
                    txtName.Text          = "";
                    txtEmail.Text         = "";
                    txtContact.Text       = "";
                    txtAddress.Text       = "";
                    txtSearchProduct.Text = "";
                    txtProductName.Text   = "";
                    txtInventory.Text     = "0";
                    txtRate.Text          = "0";
                    txtQty.Text           = "0";
                    txtSubTotal.Text      = "0";
                    txtDiscount.Text      = "0";
                    txtVat.Text           = "0";
                    txtGrandTotal.Text    = "0";
                    txtPaidAmount.Text    = "0";
                    txtReturnAmount.Text  = "0";
                }
                else
                {
                    //Transaction Failed
                    MessageBox.Show("Transaction Failed");
                }
            }
        }