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++)
{
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.