0

我在向我的数据库中插入记录时遇到问题,错误提示:“无法将值 NULL 插入到列‘OrderNo’,表‘Receipt.dbo.Orders’;列不允许空值。插入失败。然后语句有被终止”,但是当我检查我的代码时,我似乎找不到任何错误。有人可以帮助我吗?先感谢您:

这是我的一组代码:

-保存销售订单-

private void SaveSalesOrder(string status)

        {                
            int nOrder = 0;

            CloseConnection();  

            OpenConnection();

            trnOrder = cn.BeginTransaction();

            SqlCommand cmdInsert = new SqlCommand();

            try
            {
                cmdInsert.Connection = cn;
                cmdInsert.Transaction = trnOrder;
                cmdInsert.CommandType = CommandType.Text;
                cmdInsert.CommandText =
                    "INSERT INTO Orders " +
                    "(OrderDate, CustomerNo, CustomerName, CustomerAddress, PurchaseOrderNo, AgentName, Status) " +
                    "VALUES ('" +
                    dtpOrderDate.Value.Date.ToString() + "', '" +
                    txtCustomerNo.Text + "', '" +
                    txtCustomerName.Text + "', '" +
                    txtCustomerAddress.Text + "', '" +
                    txtPONo.Text + "', '" +
                    cboAgentName.Text + "', '" +
                    status + "'); " +
                    "SELECT TOP 1 OrderNo FROM Orders " +
                    "ORDER BY OrderNo DESC;";

                nOrder = Convert.ToInt16(cmdInsert.ExecuteScalar().ToString());

                for (int nRow = 0;
                    nRow <= dsDetail.Tables["OrderDetails"].Rows.Count - 1;
                    nRow++) 
                {                      

                    double dQuantity = Convert.ToDouble(dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["Quantity"].ToString());

                    string strUnit = dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["Unit"].ToString();

                    int nProductNo = Convert.ToInt16(dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["ProductNo"].ToString());

                    string strProductName = dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["ProductName"].ToString();

                    string strProductSize = dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["ProductSize"].ToString();

                    string strPackagingInside = dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["PackagingInside"].ToString();

                    double dSellingDiscount = Convert.ToDouble(dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["SellingDiscount"].ToString());

                    double dSellingPrice = Convert.ToDouble(dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["SellingPrice"].ToString());

                    double nAmount = Convert.ToDouble(dsDetail.Tables["OrderDetails"].
                        Rows[nRow]["Amount"].ToString());

                    SqlCommand cmdInsertDetail = new SqlCommand();

                    cmdInsertDetail.Connection = cn;
                    cmdInsertDetail.Transaction = trnOrder;
                    cmdInsertDetail.CommandType = CommandType.Text;
                    cmdInsertDetail.CommandText =
                        "INSERT INTO OrderDetails " +
                        "(OrderNo, PackagingOutside, Quantity, Unit, ProductNo, ProductName, " +
                        "ProductSize, PackagingInside, SellingDiscount, SellingPrice, Amount) " +
                        "VALUES ('" +
                        nOrder + "', '" +
                        dPackagingOutside + "', '" +
                        dQuantity + "', '" +
                        strUnit + "', '" +
                        nProductNo + "', '" +
                        strProductName + "', '" +
                        strProductSize + "', '" +
                        strPackagingInside + "', '" +
                        dSellingDiscount + "', '" +
                        dSellingPrice + "', '" +
                        nAmount + "')";

                    cmdInsertDetail.ExecuteNonQuery();

                }

                trnOrder.Commit();

                if (status == "OK")
                {
                    MessageBox.Show("Transaction has been saved!", "Success",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                else
                {
                    MessageBox.Show("Transaction has been voided!", "Void Transaction",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            catch (SqlException ex)
            {
                trnOrder.Rollback(); 
                MessageBox.Show(ex.Message);
            }

            finally
            {
                cn.Close();
            }
        } 

private void btnSave_Click(object sender, EventArgs e)

    {
        if (txtCustomerNo.Text == "")
        {
            MessageBox.Show("Please select a customer first.", "Empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
            btnSearchCustomer.Focus();
            return;
        }

        if (grdDetails.Rows.Count < 1)
        {
            MessageBox.Show("Please select a product first.", "Empty",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            btnProductSearch.Focus();
            return;
        }

        SaveSalesOrder("OK");

        groupCustomer(false);
        groupProduct(false);
        CSalesInv.EnableDisable(this, false);

        CloseConnection();

        InitializeOrder();

        lblTotal.Text = "";
    }
4

3 回答 3

1

错误是从数据库生成的。数据库中的 OrderNo 列设置为不允许 NULL。我想你想要某种唯一的 ID,但勾选了“No NULLs”。

你用的是什么数据库?

于 2013-09-24T12:46:12.933 回答
0

您没有在查询中指定 OrderNo。除非这是一个IDENTITY字段,否则它不会为您自动递增。

你可以做两件事:

  • 检查数据库并确保OrderId设置为允许空值
  • 使 Orderid 成为标识列。

您可能还想使用它SQL Parameterized queries来防止 SQL 注入。

 cmdInsertDetail.CommandText = "INSERT INTO YourTable (CustomerNumber) VALUES (@CustomerNo)";

 cmdInsertDetail.Parameters.AddWithValue("@CustomerNo", txtCustomerNo.Text);

这也将比建立一个随机SQL字符串更有效。

于 2013-09-24T12:45:40.577 回答
0

一个非常酷的想法是,您可以返回标识号,让您知道该行已添加到哪个行号上。是的,如果您希望在某些情况下缺少订单号,则需要将订单号设置为允许表中出现空值。

https://qualitybusinessintelligence.com/Code/ASPDOTNET/Insert-Into-Multiple-Tables.pdf

在这里,我将标识号 (OrderID) 添加到要返回的输出的标签中,这告诉我添加行时它在哪一行:(只是想帮忙)

         [CODE]
         "', " + " '" + emp.SignedName +
         "', " + " '" + emp.SignedDate + "') SELECT SCOPE_IDENTITY(); ");
         [/CODE]
于 2020-08-01T11:08:01.353 回答