Hi,
I have a Windows Forms application with a datagridview. The data in the grid has to be inserted into a SQL datatable by means of a stored proc.
I only get the error message, if there are more than one row in the datagridview.
If I enter 1 row of data and press the button SAVE, everything works fine. But as soon as I enter a second row of data and then press the SAVE button, I get the a. m. error message. (I had to create the save button because the client does not want immediate synchronization, and I have to admit several rows, because the sum of one of the columns has to match a certain value)
This is my stored procedure:
ALTER PROCEDURE [elf].[uspProduktionsDetails_Insert](
@FS_Produktion int,
@ArtZeitbedarf nchar(50),
@Dauer int,
@Meter int = 0,
@Ausschuss int = 0,
@Seite varchar(10)= '',
@Bemerkung nvarchar(max) = '')
AS
SET NOCOUNT ON;
INSERT
INTO tblProduktionsDetails
(FS_Produktion
, Art_des_Zeitbedarfs
, Dauer_in_Minuten
, Produzierte_Meter
, Produzierter_Ausschuss
, Welche_Seite
, Bemerkung)
VALUES (@FS_Produktion
,@ArtZeitbedarf
,@Dauer
,@Meter
,@Ausschuss
,@Seite
,@Bemerkung)
GO
And this is my c# code:
public void Details()
{
string spString = "uspProduktionsDetails_Insert";
SqlConnection con = new SqlConnection(Settings.Default.myConnectionString);
SqlCommand cmd = new SqlCommand(spString, con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
if (dgvPD.Rows.Count > 0)
{
for (int i = 0; i < dgvPD.Rows.Count - 1; i++)
{
_ = cmd.Parameters.AddWithValue("@FS_Produktion", Convert.ToInt32(textBoxProduktions_ID.Text));
_ = cmd.Parameters.AddWithValue("@ArtZeitbedarf", Convert.ToString(dgvPD.Rows[i].Cells[0].Value));
_ = cmd.Parameters.AddWithValue("@Dauer", Convert.ToInt32(dgvPD.Rows[i].Cells[1].Value));
_ = cmd.Parameters.AddWithValue("@Meter", Convert.ToInt32(dgvPD.Rows[i].Cells[2].Value));
_ = cmd.Parameters.AddWithValue("@Ausschuss", Convert.ToInt32(dgvPD.Rows[i].Cells[3].Value));
_ = cmd.Parameters.AddWithValue("@Seite", Convert.ToString(dgvPD.Rows[i].Cells[4].Value));
_ = cmd.Parameters.AddWithValue("@Bemerkung", Convert.ToString(dgvPD.Rows[i].Cells[5].Value));
con.Open();
_ = cmd.ExecuteNonQuery();
con.Close();
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
What I have tried:
I provided the last 4 parameters with optional values, so that even if these values were not provided there should not be an error.
I tripplechecked my parameters and the c# parameters but could not find any difference.
The error is thrown after executing the first row, the data from the first row in the grid gets into the SQL data table, while the others don't.
I would be really happy if you saw what I can't.
rgds,
Susanne