I'm trying to update a column when the value in the column is higher as 0, otherwise it INSERTS a new row.
The UPDATE query is tested in Access first and it worked, tried it in C# and it doens't.
Don't know where to look for. Any help/tips?
Reservation selection = (Reservation)lstbReservations.SelectedItem;
connection.Open();
command.CommandText = @"SELECT Service_Overzicht.OverzichtID, Service.ServiceID, Service.Beschrijving,
IIf(Service_Overzicht.Aantal Is Null,0,Service_Overzicht.Aantal) AS Aantal, Service.Prijs, Service.Categorie
FROM Service LEFT JOIN (SELECT * FROM Service_Overzicht WHERE OverzichtID = [@waarde])
AS OSF ON Service.ServiceID = OSF.ServiceID;";
command.Connection = connection;
command.Parameters.Add(new OleDbParameter("@waarde", OleDbType.Integer));
command.Parameters["@waarde"].Value = selection.Reserveringsnummer;
reader = command.ExecuteReader();
while(reader.Read())
{
if(reader.GetString(2) == "Douche Muntjes")
{
if(reader.GetInt32(3) == 0)
{
command1.CommandText = @"INSERT INTO Service_Overzicht (OverzichtID, ServiceID, Aantal) VALUES (@resID, @sID, @aantal)";
}
else
{
command1.CommandText = @"UPDATE Service_Overzicht
SET Aantal = @aantal
WHERE OverzichtID = @resID AND ServiceID = @sID";
}
command1.Connection = connection;
command1.Parameters.Add(new OleDbParameter("@resID", OleDbType.Integer));
command1.Parameters["@resID"].Value = selection.Reserveringsnummer;
command1.Parameters.Add(new OleDbParameter("@sID", OleDbType.Integer));
command1.Parameters["@sID"].Value = 1;
command1.Parameters.Add(new OleDbParameter("@aantal", OleDbType.Integer));
command1.Parameters["@aantal"].Value = nudShower.Value;
reader1 = command1.ExecuteReader();
if(reader1.HasRows)
{
MessageBox.Show("Something went wrong!");
}
else
{
MessageBox.Show("Services updated");
}
}