ExecuteNonQuery 需要打开的可用连接。连接的当前状态已关闭

-2 .net c# ado.net

我想检查发票表中的发票号是否不存在,然后添加新发票号并在发票详细信息表中填写发票详细信息,否则如果发票表中存在发票号,我想更新“总计”字段,以防发票有更多班级中\的一项以上:

\n\n
StockClass stk = new StockClass();\n\nstk.Quantity = txtQuantity.Text;\nstk.StockID = txtStockID.Text;\nstk.QtyUpdate();\nMessageBox.Show("Stock record has been Successfully updated ");\n\nInvoiceClass invclass = new InvoiceClass();\n\ntry\n{\nOleDbConnection myConnection = default(OleDbConnection);\nmyConnection = new OleDbConnection(cs);\n\nOleDbCommand myCommand = default(OleDbCommand);\n\nmyCommand = new OleDbCommand("SELECT InvoiceNo FROM Invoices WHERE InvoiceNo = @InvoiceNo", myConnection);\nOleDbParameter invono = new OleDbParameter("@username", OleDbType.VarChar);\ninvono.Value = txtInvoiceNo.Text;\nmyCommand.Parameters.Add(invono);\n\n\nmyCommand.Connection.Open();\n\nOleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);\n\nif (myReader.Read() == true)\n{\ninvclass.InvoiceNo = txtInvoiceNo.Text;\ninvclass.Total = txtGrandTotal.Text;\ninvclass.Date = InvDate.Text;\n\ninvclass.updateinvoNumber();\n}\nelse\n{\ninvclass.InvoiceNo = txtInvoiceNo.Text;\ninvclass.Total = txtGrandTotal.Text;\ninvclass.Date = InvDate.Text;\n\ninvclass.AddNewinvoNumber();\n\ninvclass.InvoiceID = txtInvoiceNo.Text;\ninvclass.ProductID = txtProdID.Text;\ninvclass.ProName = txtProdName.Text;\ninvclass.ProType = txtProdType.Text;\ninvclass.ProSize = txtProdSize.Text;\ninvclass.Quantity = textQty.Text;\ninvclass.UnitPrice = txtPrice.Text;\ninvclass.Total = textTotal.Text;\ninvclass.Date = InvDate.Text;\ninvclass.CustName = txtCustName.Text;\ninvclass.EmpName = txtEmpName.Text;\n\ninvclass.AddNew();\n}\nif (myConnection.State == ConnectionState.Open)\n{\nmyConnection.Dispose();\n}\n}\ncatch (Exception ex)\n{\nMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\n}\n\n\nOleDbDataAdapter ad = new OleDbDataAdapter("Select ProName, ProType, ProSize, Quantity, UnitPrice, Total, CustName, EmpName, date From InvoiceDetails WHERE [InvoiceID] = ?", cs);\n\nad.SelectCommand.Parameters.Add("@InvoiceID", OleDbType.VarChar);\nad.SelectCommand.Parameters["@InvoiceID"].Value = txtInvoiceNo.Text;\n\nDataSet ds = new DataSet();\nad.Fill(ds, "Invo");\nDGV1.DataSource = ds.Tables["Invo"];\nDGV1.DataSource = ds.Tables[0];\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

当它不存在时,它运行良好,但是当它存在时,我面临错误

\n\n
\n

ExecuteNonQuery 需要打开的可用连接。连接\xe2\x80\x99s当前状态已关闭

\n
\n\n
public void updateinvoNumber()\n{\nusing (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|StoreSys.mdb"))\nusing (OleDbCommand cmd = new OleDbCommand("UPDATE [invoices] SET [InvoiceNo]=?, [Total] = ?,[Date] = ? WHERE [InvoiceNo] = ?", conn))\n{\ncmd.Parameters.AddWithValue("p0", InvoiceNo);\ncmd.Parameters.AddWithValue("p1", Total);\ncmd.Parameters.AddWithValue("p2", Date);\n\n\n\ncmd.ExecuteNonQuery();\nconn.Close();\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n

sst*_*tan 5

我想我应该将此作为答案发布......

你没有打开你的连接,简单明了。

代替...

using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|StoreSys.mdb"))
using (OleDbCommand cmd = new OleDbCommand("UPDATE [invoices] SET [InvoiceNo]=?, [Total] = ?,[Date] = ? WHERE [InvoiceNo] = ?", conn))
{
    cmd.Parameters.AddWithValue("p0", InvoiceNo);
    cmd.Parameters.AddWithValue("p1", Total);
    cmd.Parameters.AddWithValue("p2", Date);

    cmd.ExecuteNonQuery();
    conn.Close();
}
Run Code Online (Sandbox Code Playgroud)

你需要...(注意conn.Open();

using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|StoreSys.mdb"))
{
    conn.Open(); // <-- You forgot this.
    using (OleDbCommand cmd = new OleDbCommand("UPDATE [invoices] SET [InvoiceNo]=?, [Total] = ?,[Date] = ? WHERE [InvoiceNo] = ?", conn))
    {
        cmd.Parameters.AddWithValue("p0", InvoiceNo);
        cmd.Parameters.AddWithValue("p1", Total);
        cmd.Parameters.AddWithValue("p2", Date);

        cmd.ExecuteNonQuery();
        // conn.Close(); <-- you don't need this btw. This will happen automatically as you exit the "using" block.
    }
}
Run Code Online (Sandbox Code Playgroud)