I have some problems when trying to update values in my SQL Server database.
I have the following DataGridView
:
I want to change the checkbox then write the boolean
back to SQL Server when I hit the save button below.
I added the BindingSource
to the SQL Server table with the designer. That works fine. It reads the values from SQL and shows them.
The save button contains the following code:
private void cmdChange_Click(object sender, EventArgs e)
{
var PID = new SqlParameter("@PID",SqlDbType.Int);
var Programm = new SqlParameter("@Installed",SqlDbType.Int);
string prgUpdate = "UPDATE Programme SET Installed=@Installed WHERE PID=@PID";
using (SqlConnection update = new SqlConnection(altdbconnect))
{
update.Open();
for (int i = 0; i < dGVUpdates.Rows.Count; i++)
{
using (SqlCommand insert3 = new SqlCommand(prgUpdate, update))
{
insert3.Parameters.AddWithValue("@PID", dGVUpdates.Rows[i].Cells["pIDDataGridViewTextBoxColumn1"].Value);
insert3.Parameters.AddWithValue("@Installed", dGVUpdates.Rows[i].Cells["installedDataGridViewTextBoxColumn"].Value);
insert3.ExecuteNonQuery();
}
}
update.Close();
}
dGVUpdates.Update();
dGVUpdates.Refresh();
}
But when I hit the button to save the values I changed in the DGV, I got the following error:
Additional information: The parameterized query '(@PID nvarchar(4000),@Installed nvarchar(4000))UPDATE Programme ' expects the parameter '@PID', which was not supplied.
I compared all used statements, to another DataGridView
. They look the same.
And like in the linked picture shown, there is a parameter @PID
set.
The column pIDDataGridViewTextBoxColumn1
was generated by the designer. This is the column for PID
.
I compared it to this one that is working fine. Can't find the difference:
var kdNummer = new SqlParameter("@Kundennummer", SqlDbType.Int);
var kdName = new SqlParameter("@Kundenname", SqlDbType.VarChar);
var kdMail = new SqlParameter("@Kundenmail", SqlDbType.VarChar);
var kdTele = new SqlParameter("@Telefon", SqlDbType.VarChar);
var kdOffen = new SqlParameter("@Offen", SqlDbType.Int);
string kdquery = "INSERT INTO gfcKunden VALUES (@Kundennummer, @Kundenname, @Kundenmail, @Telefon, @Offen)";
using (SqlConnection updatedb = new SqlConnection(altdbconnect))
{
updatedb.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
try
{
using (SqlCommand insert = new SqlCommand(kdquery, updatedb))
{
insert.Parameters.AddWithValue("@Kundenname", dataGridView1.Rows[i].Cells["Kundenname"].Value);
insert.Parameters.AddWithValue("@Kundennummer", dataGridView1.Rows[i].Cells["Kundennummer"].Value);
insert.Parameters.AddWithValue("@Kundenmail", dataGridView1.Rows[i].Cells["Kundenmail"].Value);
insert.Parameters.AddWithValue("@Telefon", dataGridView1.Rows[i].Cells["Telefon"].Value);
insert.Parameters.AddWithValue("@Offen", 0);
insert.ExecuteNonQuery();
}
}
catch { }
}
updatedb.Close();
}
Screenshot from the SQL Server table: