首页 > 解决方案 > 如何使用 LINQ-to-Entities 将客户订单详细信息插入我的账单表

问题描述

请帮我解决这个问题。

我正在使用窗口窗体创建一个商店管理应用程序来管理一家婴儿用品店。除了将客户订单详细信息添加到数据库之外,所有方面都有效。

到目前为止,我使用下面的 ode 添加了包含他或她想要购买的所有物品的客户账单。

 private void btuAddBill_Click(object sender, EventArgs e)
    {
        if (txtProName.Text != "" && txtProPrice.Text != "")
        {
            if (ComboSelectCustID.Text != "NONE")
            {
                if (txtQuantity.Text != "")
                {
                    decimal Total = Convert.ToDecimal(txtProPrice.Text) * Convert.ToDecimal(txtQuantity.Text);
                    DataGridViewRow newRow = new DataGridViewRow();
                    newRow.CreateCells(DataGribBills);
                    newRow.Cells[0].Value = n + 1;
                    newRow.Cells[1].Value = txtProName.Text;
                    newRow.Cells[2].Value = txtProPrice.Text;
                    newRow.Cells[3].Value = txtQuantity.Text;
                    newRow.Cells[4].Value = Convert.ToDecimal(txtProPrice.Text) * Convert.ToDecimal(txtQuantity.Text);
                    DataGribBills.Rows.Add(newRow);
                    n++;
                    
                    GrdTotal = GrdTotal + Total;
                    txtTotalAmt.Text = Convert.ToString(GrdTotal);
                }
                else
                {
                    MessageBox.Show("Kindly Input Quantity to Proceed");
                }
            }
            else
            {
                MessageBox.Show("Kindly Select Customer ID");
            }

        }
        else
        {
            MessageBox.Show("Kindly Select Product");
        }
    }

当客户准备好付款时,会处理交易并将总账单金额连同有关交易的一些详细信息发送到数据库。

下面是我用来将交易发布到我的数据库的代码。此交易仅包含所购商品清单的总价以及总数量。

private void btuAddTransaction_Click(object sender, EventArgs e)
    {
        TotaPurchaseAmt = Convert.ToDecimal(txtQuantity.Text) * Convert.ToDecimal(txtPurchaseCost.Text);
        decimal profit = GrdTotal - TotaPurchaseAmt;
        DBS = new BabyHubEntities();
        ProductTB del = new ProductTB();
        decimal proID = Convert.ToDecimal(txtTbale_ID.Text);
        cusQua = Convert.ToDecimal(txtQuantity.Text);
        var IDpro = (from s in DBS.ProductTBs where s.t_ID == proID select s).FirstOrDefault();

        IDpro.ProdQty = IDpro.ProdQty - Convert.ToDecimal(txtQuantity.Text);
        if (cusQua > IDpro.ProdQty)
        {
            MessageBox.Show("Qunatity is not in stock");
        }
        else
        {
            DBS.SaveChanges();
            if (txtBill_ID.Text == "")
            {
                MessageBox.Show("Missing Transaction Details");
            }
            else
            {
                try
                {
                    DBS = new BabyHubEntities();
                    BillingTB acc = new BillingTB();
                    ProfitTB PS = new ProfitTB();

                    acc.bill_ID = Convert.ToDecimal(hidetxtHideID.Text);
                    acc.Biiling_ID = txtBill_ID.Text;
                    acc.billDate = DateTime.Now;
                    acc.customerID = ComboSelectCustID.Text;
                    acc.customerName = txtCustName.Text;
                    acc.EmplName = MainLogin.TBH_Login.Username;
                    acc.Amt = Convert.ToDecimal(txtTotalAmt.Text);

                    string message = "Are you sure you want to add this Transaction?";
                    string title = "Adding Record...";
                    MessageBoxButtons but = MessageBoxButtons.YesNo;
                    DialogResult result = MessageBox.Show(message, title, but);
                    if (result == DialogResult.Yes)
                    {
                        acc.SellerName = MainLogin.TBH_Login.Username;
                        DBS.BillingTBs.Add(acc);
                        DBS.SaveChanges();
                        MessageBox.Show("Transaction Added Successfully");
                    }
                    else
                    {
                        MessageBox.Show("Transaction Not Added");
                    }

                    PS.BillID = txtBill_ID.Text;
                    PS.SoldBy = MainLogin.TBH_Login.Username;
                    PS.SalAmt = GrdTotal;
                    PS.ProfitAmt = profit;
                    PS.SoldQty = Convert.ToDecimal(txtQuantity.Text);
                    PS.SaleDT = DateTime.Now;
                    PS.UnitPrice = Convert.ToDecimal(txtProPrice.Text);
                    PS.ProdPurcCost = Convert.ToDecimal(txtPurchaseCost.Text);
                    DBS.ProfitTBs.Add(PS);
                    DBS.SaveChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        DisplayTransaction();
    }

现在,我想将订单详细信息发送到另一张表,其中包括客户购买的商品列表以及他们的单价和数量,所有这些都在一个带有订单日期的 OrderID 下。这让我好几天没睡。

请协助我。

标签: c#winformslinqlinq-to-entities

解决方案


推荐阅读