C# 代码使用 SQL 成功执行,但在数据库中检查时,它们仍然显示空值

时间:2021-06-25 15:21:41

标签: c# sql

**我的数据必须从各种文本框中保存,但发生的情况是,当我尝试保存时没有错误消息,但对我来说问题是数据没有发布到 SQL 数据库, 在 SQL 表中检查时,它的所有值都显示为空

我的保存按钮功能如下**

 private void btnsavebill_Click(object sender, EventArgs e)
    {
        transactionBLL transaction = new transactionBLL();
        transaction.type = lblpurchasehead.Text;

        //get the ID of the dealer or customer HEre
        // get the name of the dealer or custoemr name
        string deacustname = txtdlrname.Text;
        dealerBLL dc = dcdal.getdeacustIDfromName(deacustname);

        transaction.date_cust_id = dc.id;
        transaction.grandtotal = Math.Round(decimal.Parse(txtgrandtotal.Text), 2);
        transaction.transaction_date = DateTime.Now;
        transaction.tax = decimal.Parse(txtgstper.Text);
        transaction.discount = decimal.Parse(txtDiscountbill.Text);

        //get user name of logged in user name
        string username = frmLogin.loggedIn;
        userBLL u = udal.GetIDFromUsername(username);

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

        // creating a boolean variable and set value to false
        bool success = false;

        //Actual cocde to insert trans and trans DT

        using TransactionScope scope = new TransactionScope();
        {
            int transactionId = -1;

            bool w = tdal.Insert_Transaction(transaction, out transactionId);


            //use for loop to insert transaction details

            for (int i = 0;i<transactionDT.Rows.Count;i++)
            {
                //get all the details of products
                transactiondetailBLL transactiondetail = new transactiondetailBLL();

                //get the product name and convert it into ID
                string ProductName = transactionDT.Rows[i][0].ToString();

                productsBLL p = pdal.getproductsIDfromName(ProductName);
                
                transactiondetail.product_int = p.id;
                transactiondetail.rate = decimal.Parse(transactionDT.Rows[i][1].ToString());
                transactiondetail.qty = decimal.Parse(transactionDT.Rows[i][2].ToString());
                transactiondetail.total = Math.Round(decimal.Parse(transactionDT.Rows[i][3].ToString()), 2);
                transactiondetail.dec_cust_id = dc.id;
                transactiondetail.added_date = DateTime.Now;
                transactiondetail.added_by = u.id;

                //Insert transaction Detail inside our Database
                bool y = tddal.InsertTransactionDetail(transactiondetail);
                success = w && y;
            }
            if (success == true)
            {
                //transaction complete
                MessageBox.Show("Transaction completed successfully to details table");
                //clear data grid view
                dgvaddprodforbill.DataSource = null;
                dgvaddprodforbill.Rows.Clear();

                //clear all other values

                txtdlrSearch.Text = "";
                txtdlremail.Text = "";
                txtdlrname.Text = "";
                txtdlrcontact.Text = "";
                txtdlraddress.Text = "";
                txtproductsearch.Text = "";
                txtproname.Text = "";
                txtproinventory.Text = "";
                txtproprice.Text = "";
                txtproqty.Text = "";
                txtsubtotal.Text = "0";
                txtDiscountbill.Text = "0";
                txtgstper.Text = "0";
                txtgrandtotal.Text = "0";
                txtpaidamount.Text = "0";



            }
            else
            {
                //transaction failed
                MessageBox.Show("Transaction completed failed to details table");
            }

我的数据访问层也如下

#region Insert Transaction MEthod

    public  bool  Insert_Transaction(transactionBLL t, out int transactionID)
    {
        bool issucess = false;

        transactionID = -1;

        SqlConnection connection = new SqlConnection(myconnstring);


        try
        {
            //sql query to insert transactions
         
            string sql = "INSERT INTO tbl_transaction (type, date_cust_id, grandtotal, transaction_date, tax, discount, added_by) VALUES (@type, @date_cust_id, @grandtotal, @transaction_date, @tax, @discount, @added_by);SELECT @@IDENTITY;";
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.Parameters.AddWithValue("@type", t.type);
            cmd.Parameters.AddWithValue("@date_cust_id", t.date_cust_id);
            cmd.Parameters.AddWithValue("@grandtotal", t.grandtotal);
            cmd.Parameters.AddWithValue("@transaction_date", t.transaction_date);
            cmd.Parameters.AddWithValue("@tax", t.tax);
            cmd.Parameters.AddWithValue("@discount", t.discount);
            cmd.Parameters.AddWithValue("added_by", t.added_by);

            connection.Open();

            object o = cmd.ExecuteScalar();
            
            //if the query is executed successfully then the value will not be null or vice versa

            if (o!=null)
            {
                transactionID = int.Parse(o.ToString());
                issucess = true;
                
            }
            else
            {
                issucess = false;
            }
            }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            connection.Close();
        }

        return issucess;
    }

    #endregion

请找到一种方法,因为我不能,因为我是一个没有任何编码背景罐的人,这是我第一次尝试观看各种 YouTube 视频和教程,我不想因为我的错误而被卡住< /p>

0 个答案:

没有答案
相关问题