如何修复此错误 [英] How Can fixe This Error
问题描述
这是我的商店Procuder:
ALTER proc [dbo]。[insertInvoiceSaleDetails]
@ FK_Invoice_id int,
@FK_Customers_id int,
@FK_Product_id int,
@Product_name nvarchar(150),
@QTY int,
@PriceUnit float,
@PriceDolar float,
@TotalPrice float,
@Discount漂浮,
@TotalInvoice浮动,
@Paid浮动,
@Remainder浮动
as
开始
插入InvoiceSaleDetails值(@ FK_Invoice_id,@ FK_Customers_id,@ FK_Product_id,@ Product_name,
@ QTY,@ PriceUnit,@ PriceDolar,@ TotalPrice, @折扣,@ TotalInvoice,@付费,@ Remainder)
结束
这个我的代码:
string CS = ConfigurationManager.ConnectionStrings [DBCS]。ConnectionString;
using(SqlConnection con2 = new SqlConnection(CS))
{
SqlCommand cmd2 =新的SqlCommand(insertInvoiceSaleDetails,con2);
for(int y = 0; y< dataGridView1.Rows.Count - 1; y ++)
{
cmd2.CommandType = System.Data.CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue(@ FK_Invoice_id,Convert.ToInt32(txtInvoiceId.Text));
cmd2.Parameters.AddWithValue(@ FK_Customers_id,txtCustomersID.Text);
cmd2.Parameters.AddWithValue(@ FK_Product_id,Convert.ToInt32(dataGridView1.Rows [y] .Cells [0] .Value));
cmd2.Parameters .AddWithValue(@ Product_name,dataGridView1.Rows [y] .Cells [1] .Value.ToString());
cmd2.Parameters.AddWithValue(@ QTY,dataGridView1.Rows [ y] .Cells [2] .Value);
cmd2.Parameters.AddWithValue(@ PriceUnit,dataGridView1.Rows [y] .Cells [3] .Value);
cmd2.Parameters.AddWithValue(@ PriceDolar,dataGridView1.Rows [ y] .Cells [4] .Value);
cmd2.Parameters.AddWithValue(@ TotalPrice,dataGridView1.Rows [y] .Cells [5] .Value);
cmd2.Parameters.AddWithValue(@ Discount,dataGridView1.Rows [y] .Cells [6] .Value);
cmd2.Parameters.AddWithValue(@ TotalInvoice,dataGridView1。行[y] .Cells [7] .Value);
cmd2.Parameters.AddWithValue(@ Paid,dataGridView1.Rows [y] .Cells [8] .Value);
cmd2.Parameters.AddWithValue(@ Remainder,dataGridView1.Rows [y] .Cells [9] .Value);
}
con2.Open();
cmd2.ExecuteNonQuery();
错误是:
过程或函数'insertInvoiceSaleDetails'需要参数'@FK_Invoice_id',这是未提供的。
This My Store Procuder :
ALTER proc [dbo].[insertInvoiceSaleDetails]
@FK_Invoice_id int,
@FK_Customers_id int,
@FK_Product_id int,
@Product_name nvarchar(150),
@QTY int,
@PriceUnit float,
@PriceDolar float,
@TotalPrice float,
@Discount float,
@TotalInvoice float,
@Paid float,
@Remainder float
as
begin
insert into InvoiceSaleDetails values (@FK_Invoice_id,@FK_Customers_id,@FK_Product_id,@Product_name,
@QTY,@PriceUnit,@PriceDolar,@TotalPrice,@Discount,@TotalInvoice,@Paid,@Remainder)
end
This My code:
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con2 = new SqlConnection(CS))
{
SqlCommand cmd2 = new SqlCommand("insertInvoiceSaleDetails", con2);
for (int y = 0; y < dataGridView1.Rows.Count - 1; y++)
{
cmd2.CommandType = System.Data.CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@FK_Invoice_id",Convert.ToInt32( txtInvoiceId.Text));
cmd2.Parameters.AddWithValue("@FK_Customers_id", txtCustomersID.Text);
cmd2.Parameters.AddWithValue("@FK_Product_id", Convert.ToInt32(dataGridView1.Rows[y].Cells[0].Value));
cmd2.Parameters.AddWithValue("@Product_name", dataGridView1.Rows[y].Cells[1].Value.ToString());
cmd2.Parameters.AddWithValue("@QTY", dataGridView1.Rows[y].Cells[2].Value);
cmd2.Parameters.AddWithValue("@PriceUnit", dataGridView1.Rows[y].Cells[3].Value);
cmd2.Parameters.AddWithValue("@PriceDolar", dataGridView1.Rows[y].Cells[4].Value);
cmd2.Parameters.AddWithValue("@TotalPrice", dataGridView1.Rows[y].Cells[5].Value);
cmd2.Parameters.AddWithValue("@Discount", dataGridView1.Rows[y].Cells[6].Value);
cmd2.Parameters.AddWithValue("@TotalInvoice", dataGridView1.Rows[y].Cells[7].Value);
cmd2.Parameters.AddWithValue("@Paid", dataGridView1.Rows[y].Cells[8].Value);
cmd2.Parameters.AddWithValue("@Remainder", dataGridView1.Rows[y].Cells[9].Value);
}
con2.Open();
cmd2.ExecuteNonQuery();
The error is:
Procedure or function 'insertInvoiceSaleDetails' expects parameter '@FK_Invoice_id', which was not supplied.
试试这个:
Try this:
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con2 = new SqlConnection(CS))
using (SqlCommand cmd2 = new SqlCommand("insertInvoiceSaleDetails", con2))
{
cmd2.CommandType = System.Data.CommandType.StoredProcedure;
con2.Open();
for (int y = 0; y < dataGridView1.Rows.Count; y++)
{
// Clear the parameters from the previous row, if any:
cmd2.Parameters.Clear();
cmd2.Parameters.AddWithValue("@FK_Invoice_id",Convert.ToInt32( txtInvoiceId.Text));
cmd2.Parameters.AddWithValue("@FK_Customers_id", txtCustomersID.Text);
cmd2.Parameters.AddWithValue("@FK_Product_id", Convert.ToInt32(dataGridView1.Rows[y].Cells[0].Value));
cmd2.Parameters.AddWithValue("@Product_name", dataGridView1.Rows[y].Cells[1].Value.ToString());
cmd2.Parameters.AddWithValue("@QTY", dataGridView1.Rows[y].Cells[2].Value);
cmd2.Parameters.AddWithValue("@PriceUnit", dataGridView1.Rows[y].Cells[3].Value);
cmd2.Parameters.AddWithValue("@PriceDolar", dataGridView1.Rows[y].Cells[4].Value);
cmd2.Parameters.AddWithValue("@TotalPrice", dataGridView1.Rows[y].Cells[5].Value);
cmd2.Parameters.AddWithValue("@Discount", dataGridView1.Rows[y].Cells[6].Value);
cmd2.Parameters.AddWithValue("@TotalInvoice", dataGridView1.Rows[y].Cells[7].Value);
cmd2.Parameters.AddWithValue("@Paid", dataGridView1.Rows[y].Cells[8].Value);
cmd2.Parameters.AddWithValue("@Remainder", dataGridView1.Rows[y].Cells[9].Value);
cmd2.ExecuteNonQuery();
}
}
我假设你想要为网格中的每个行执行命令。你的代码正在跳过最后一行,并且只使用倒数第二行的值执行一次命令。
I've assumed you want to execute the command for every row in the grid. Your code was skipping the last row, and was only executing the command once, using the values from the penultimate row.
这篇关于如何修复此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!