Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将空值插入表Orders_C#_Sql - Fatal编程技术网

C# 无法将空值插入表Orders

C# 无法将空值插入表Orders,c#,sql,C#,Sql,我在将记录插入数据库时遇到问题, 错误为:“无法将值NULL插入到“OrderNo”列、表“Receipt.dbo.Orders”中;列不允许为NULL。插入失败。然后语句已终止”,但当我检查代码时,似乎找不到任何错误。有人能帮我吗?提前感谢您: 以下是我的代码集: -用于保存销售订单- private void SaveSalesOrder(string status) { int nOrder = 0;

我在将记录插入数据库时遇到问题, 错误为:“无法将值NULL插入到“OrderNo”列、表“Receipt.dbo.Orders”中;列不允许为NULL。插入失败。然后语句已终止”,但当我检查代码时,似乎找不到任何错误。有人能帮我吗?提前感谢您:

以下是我的代码集:

-用于保存销售订单-

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 = "";
    }
private void SaveSalesOrder(字符串状态)
{                
int-nOrder=0;
CloseConnection();
OpenConnection();
trnOrder=cn.BeginTransaction();
SqlCommand cmdInsert=new SqlCommand();
尝试
{
cmdInsert.Connection=cn;
cmdInsert.Transaction=trnOrder;
cmdInsert.CommandType=CommandType.Text;
cmdInsert.CommandText=
“插入订单”+
(订单日期、客户编号、客户名称、客户地址、采购订单号、代理名称、状态)+
“值('”+
dtpOrderDate.Value.Date.ToString()+“,”+
txtCustomerNo.Text+“,”+
txtCustomerName.Text+“,”+
txtCustomerAddress.Text+“,”+
txtPONo.Text+“,”+
cboAgentName.Text+“,”+
状态+“')+
“从订单中选择前1个订单号”+
“按订单订购无需说明;”;
nOrder=Convert.ToInt16(cmdInsert.ExecuteScalar().ToString());
对于(int nRow=0;

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

你可以做两件事:

  • 检查数据库并确保
    OrderId
    设置为允许空值
  • 将Orderid设置为标识列
您可能还想使用
SQL参数化查询
来防止SQL注入

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

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

这也比构建随机的
SQL
字符串更有效。

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


您使用的是什么数据库?

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

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


您可以发布订单表设计的屏幕截图吗?很可能它设置为不允许nullsTry手动在数据库中插入空值。检查它的主键!!您可能想更改您的
OrderNo
列以具有标识关系(如果它是Microsoft SQL Server),或be
auto_increment
如果是MySQL,请研究“sql注入”和“参数”是的,OrderNo在主键中,这就是为什么它被设置为不允许空的原因。哪个供应商?MySQL、Oracle、Microsoft?
nOrder=Convert.ToInt16(cmdInsert.ExecuteScalar().ToString())我想诺德不能给空值。
         [CODE]
         "', " + " '" + emp.SignedName +
         "', " + " '" + emp.SignedDate + "') SELECT SCOPE_IDENTITY(); ");
         [/CODE]