Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 14-Apr-15 7:10am
v2
Comments
Richard Deeming 14-Apr-15 14:06pm    
How many rows are in dataGridView1? I'm betting there's only one, so your for loop never executes.

Also, if you want to execute the command for each row, you'll need to move the con.Open() above the start of the for loop, and the cmd2.ExecuteNonQuery() inside the for loop.
Member 11280947 14-Apr-15 15:19pm    
But there is no error and no Insert into database
Richard Deeming 14-Apr-15 15:25pm    
If there is no error, then why did you post an error message as part of your question?!
Member 11280947 14-Apr-15 15:29pm    
I put con.Open() above the start of the for loop, and the cmd2.ExecuteNonQuery() inside the for loop.
and there no insert into database
Richard Deeming 14-Apr-15 15:31pm    
Did you fix the condition within the loop, so that it loops over all of the rows?

1 solution

Try this:
C#
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.
 
Share this answer
 
v2
Comments
Member 11280947 14-Apr-15 15:14pm    
Same Error
Richard Deeming 14-Apr-15 15:19pm    
Not if you've copied the code from my answer.
Member 11280947 14-Apr-15 15:21pm    
What Do you mean
Richard Deeming 14-Apr-15 15:24pm    
I mean, if you've copied the code from my answer, and not changed it, then you won't be getting exactly the same error.

If you're still getting exactly the same error, then either you've changed the code I posted, or there's something else going on that you haven't mentioned.

If you're getting a slightly different error, then you need to post the new error message.
Member 11280947 14-Apr-15 15:27pm    
if you have another way to insert data from dataGridView1 Tell me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900