Click here to Skip to main content
15,891,431 members

Comments by Abdelrahman-Dev (Top 6 by date)

Abdelrahman-Dev 25-Aug-21 9:45am View    
Thanks, It worked but I changed the AddWithValue to Add, Is there a difference ?
Again thank you so much for the answer
Abdelrahman-Dev 25-Aug-21 9:36am View    
Deleted
So it should be like that? :
foreach (DataGridViewRow r in dataGridView1.Rows)
{
using (var command2 = new OleDbCommand("UPDATE SoldProducts SET Amount = Amount + @Amount WHERE BookName = @BookName", connection))
{
command2.Parameters.AddWithValue("@Amount", r.Cells[1].Value);
command2.Parameters.AddWithValue("@BookName", r.Cells[0].Value);
connection.Open();
int rowsAffected = command2.ExecuteNonQuery();
if (rowsAffected == 0)
{
command2.Parameters.Clear();
command2.CommandText = "INSERT INTO SoldProducts (BookName, Amount, Price) VALUES (@BookName, @Amount, @Price)";
command2.Parameters.AddWithValue("@BookName", r.Cells[0].Value);
command2.Parameters.AddWithValue("@Amount", r.Cells[1].Value);
command2.Parameters.AddWithValue("@Price", r.Cells[4]);
command2.ExecuteNonQuery();
}
connection.Close();
}
}
Abdelrahman-Dev 17-Aug-21 15:21pm View    
Thanks, I will try this.
But why am I getting too many downvotes, why not someone like you just telling me that a stored procedure wouldn't work with Access
Abdelrahman-Dev 15-Aug-21 14:01pm View    
So why does it throw that exception?
Abdelrahman-Dev 15-Aug-21 12:35pm View    
Sorry I just did something wrong in the query. Here is my whole query :
@"CREATE PROCEDURE InsertOrUpdate
@ProductName NVARCHAR(MAX),
@Amount INT,
@Price DECIMAL
AS
BEGIN
UPDATE SoldProducts SET Amount = Amount + @amount WHERE BookName = @ProductName
IF @@ROWCOUNT=0
INSERT INTO SoldProducts (ProductName, Amount, Price) VALUES (@productName, @amount, @price)
END"
And here is what the exception actually is :
Invalid SQL syntax - expected token: AS