private void comboItemName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string keyword = comboItemName.Text;
            ChallanReturnDetailsBLL crBLL = ChallanReturnDetailsDAL.GetProductForChallanReturn(keyword);

            comboBoxUnit.Text    = crBLL.Unit;
            textQuantity.Text    = crBLL.Qty.ToString();
            textRate.Text        = crBLL.Rate.ToString();
            textDiscount.Text    = crBLL.Discount_Per.ToString();
            comboGstType.Text    = crBLL.GST_Type;
            textTotalAmount.Text = crBLL.Total.ToString();
        }
        public bool insertSalesReturndetails(ChallanReturnDetailsBLL crdBLL)
        {
            bool isSuccess = false;

            SqlConnection con = new SqlConnection(myconnstrng);

            try
            {
                //inserting transaction details
                string     sql = "INSERT INTO SalesReturn_Transactions_Details (Invoice_No,Product_ID,Cust_ID,Product_Name,Unit,Qty,Rate,Dicount_Per,GST_Type,GST_Per,Total) VALUES(@Invoice_No,@Product_ID,@Cust_ID,@Product_Name,@Unit,@Qty,@Rate,@Discount_Per,@GST_Type,@GST_Per,@Total)";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@Invoice_No", crdBLL.Invoice_No);
                cmd.Parameters.AddWithValue("@Product_ID", crdBLL.Product_ID);
                cmd.Parameters.AddWithValue("@Cust_ID", crdBLL.Cust_ID);
                cmd.Parameters.AddWithValue("@Product_Name", crdBLL.Product_Name);
                cmd.Parameters.AddWithValue("@Unit", crdBLL.Unit);
                cmd.Parameters.AddWithValue("@Qty", crdBLL.Qty);
                cmd.Parameters.AddWithValue("@Rate", crdBLL.Rate);
                cmd.Parameters.AddWithValue("@Discount_Per", crdBLL.Discount_Per);
                cmd.Parameters.AddWithValue("@GST_Type", crdBLL.GST_Type);
                cmd.Parameters.AddWithValue("@GST_Per", crdBLL.GST_Per);
                cmd.Parameters.AddWithValue("@Total", crdBLL.Total);
                //Unit,Qty,Rate,Discount_Per,GST_Type,GST_Per,Total
                con.Open();

                int rows = cmd.ExecuteNonQuery();

                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }
            return(isSuccess);
        }
        public ChallanReturnDetailsBLL GetProductForChallanReturn(string keyword)
        {
            ChallanReturnDetailsBLL crBLL = new ChallanReturnDetailsBLL();
            SqlConnection           conn  = new SqlConnection(myconnstrng);
            DataTable dt = new DataTable();

            try
            {
                string         sql     = "select * from Challan_Transactions_Details where Product_Name LIKE'%" + keyword + "%' ";
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);

                conn.Open();

                adapter.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    crBLL.Unit         = dt.Rows[0]["Unit"].ToString();
                    crBLL.Qty          = decimal.Parse(dt.Rows[0]["Qty"].ToString());
                    crBLL.Rate         = decimal.Parse(dt.Rows[0]["Rate"].ToString());
                    crBLL.Discount_Per = decimal.Parse(dt.Rows[0]["Dicount_Per"].ToString());
                    crBLL.GST_Type     = dt.Rows[0]["GST_Type"].ToString();
                    crBLL.GST_Per      = decimal.Parse(dt.Rows[0]["GST_Per"].ToString());
                    crBLL.Total        = decimal.Parse(dt.Rows[0]["Total"].ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return(crBLL);
        }
        public void save()
        {
            string sname = textCust_Name.Text;

            if (comboTransactionType.Text != "")
            {
                if (sname != "")
                {
                    if (dgvAddedProducts.Rows.Count != 0)
                    {
                        if (comboReturnReson.Text != "")
                        {
                            customerBLL c = cDAL.getCustomerIdFromName(sname);

                            decimal subTotal, totalDiscount, totalSgst, totalCgst, totalIgst, grandTotal;

                            int invoceNo;
                            int.TryParse(comboInvoiceNo.Text, out invoceNo);


                            string type = comboTransactionType.Text;
                            decimal.TryParse(textSubTotal.Text, out subTotal);
                            decimal.TryParse(textSubDiscount.Text, out totalDiscount);
                            decimal.TryParse(textSgst.Text, out totalSgst);
                            decimal.TryParse(textCgst.Text, out totalCgst);
                            decimal.TryParse(textIgst.Text, out totalIgst);
                            decimal.TryParse(textGrandTotal.Text, out grandTotal);

                            string reson = comboReturnReson.Text;

                            ChallanReturnBLL.SalesID          = invoceNo;
                            ChallanReturnBLL.Transaction_Type = type;
                            ChallanReturnBLL.Cust_ID          = c.Cust_ID;
                            ChallanReturnBLL.Sub_Total        = subTotal;
                            ChallanReturnBLL.TDiscount        = totalDiscount;
                            ChallanReturnBLL.TSGST            = totalSgst;
                            ChallanReturnBLL.TCGST            = totalCgst;
                            ChallanReturnBLL.TIGST            = totalIgst;
                            ChallanReturnBLL.Grand_Total      = grandTotal;
                            ChallanReturnBLL.Reson            = reson;

                            ChallanReturnBLL.SalesDetails = salesReturnDT;
                            bool isSuccess = false;

                            // using (TransactionScope scope = new TransactionScope())

                            //int Invoice_No = -1; alredy declared on top
                            bool b = ChallanReturnDAL.insertSalesReturn(ChallanReturnBLL, out Invoice_No);

                            for (int i = 0; i < salesReturnDT.Rows.Count; i++)
                            {
                                ChallanReturnDetailsBLL crdBLL = new ChallanReturnDetailsBLL();

                                stockBLL         stockBLL    = new stockBLL();
                                string           productName = salesReturnDT.Rows[i][1].ToString();
                                ProductMasterBLL p           = ProductMasterDAL.GetProductIDFromName(productName);

                                crdBLL.Product_ID   = p.Product_ID;
                                crdBLL.Invoice_No   = Invoice_No;
                                crdBLL.Cust_ID      = c.Cust_ID;
                                crdBLL.Product_Name = salesReturnDT.Rows[i][1].ToString();
                                crdBLL.Unit         = salesReturnDT.Rows[i][2].ToString();
                                crdBLL.Qty          = Math.Round(decimal.Parse(salesReturnDT.Rows[i][3].ToString()), 2);
                                crdBLL.Rate         = Math.Round(decimal.Parse(salesReturnDT.Rows[i][4].ToString()), 2);
                                crdBLL.Discount_Per = Math.Round(decimal.Parse(salesReturnDT.Rows[i][5].ToString()), 2);
                                crdBLL.GST_Type     = salesReturnDT.Rows[i][6].ToString();
                                crdBLL.GST_Per      = Math.Round(decimal.Parse(salesReturnDT.Rows[i][7].ToString()), 2);
                                crdBLL.Total        = Math.Round(decimal.Parse(salesReturnDT.Rows[i][9].ToString()), 2);

                                int Product_id = p.Product_ID;
                                stockBLL.Product_Id = Product_id;
                                stockBLL.Quantity   = Math.Round(decimal.Parse(salesReturnDT.Rows[i][3].ToString()), 2);
                                stockBLL.Unit       = salesReturnDT.Rows[i][2].ToString();

                                bool y = ChallanReturnDetailsDAL.insertSalesReturndetails(crdBLL);

                                if (y == true)
                                {
                                    bool x = stockDAL.Update(stockBLL);
                                }

                                isSuccess = b && y;

                                isSuccess = true;
                            }
                            isSuccess = b;
                            if (isSuccess == true)
                            {
                                //scope.Complete();
                                MessageBox.Show("Transaction Completed");
                                //clear();
                            }
                            else
                            {
                                MessageBox.Show("Transaction Failed");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Please Select Retrun Reson");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Add product Details");
                    }
                }
                else
                {
                    MessageBox.Show("Please Select Customer Details");
                }
            }
            else
            {
                MessageBox.Show("Please Select Purchase Type GST OR NOGST");
            }
        }