I am trying to delete an old row from a sql-server-ce database (based on the primary key value), whenever that primary key value is updated.
The goal is to (once supplied with the new id) add a new entry with an insert statement and then delete the old entry. It might be important to know that the data type for the primary key (City Tag Number) within the database itself is int, and the C# variable originalCTag was declared var, but assigned “” (empty string) upon its declaration, and then assigned the string representation of the desired target primary key value.
While this should be a simple task, here’s the question:
Why is the sql action query not working? Using C# in WebMatrix, here is what I have tried:
db.Execute("DELETE FROM Vaccinations WHERE [City Tag Number] = " + originalCTag);
^This states that there was an error parsing the query at the index of the equals sign.
db.Execute("DELETE FROM Vaccinations WHERE [City Tag Number] = @0", originalCTag);
^This states that the input string was not in the correct format.
db.Execute("DELETE FROM Vaccinations WHERE [City Tag Number] = @0", int.Parse(originalCTag));
^This states that the input string was not in the correct format.
db.Execute("DELETE FROM Vaccinations WHERE [City Tag Number] = @0", originalCTag.ToString());
^This (once again) states that the input string was not in the correct format (can we not parameterize queries with a DELETE statement)?
So… lastly, I tried:
db.Execute("DELETE FROM Vaccinations WHERE [City Tag Number] = '" + originalCTag + "'");
^This doesn’t produce any errors, but also does not do what it’s supposed to (e.g., delete the row).
At this point, I’m kind of at a loss. I would expect at least one of these statements to perform the desired function, if not a few of them.
Clearly I am missing something (and I wouldn’t be surprised if that something was obvious) here. If any of these statements ‘should work’ then maybe there is something else wrong with my code before the db.Execute statement.
——————-UPDATE—————–
In case you want to see the db declaration statement:
var db = Database.Open("Vaccinations");
Yes, Vaccinations is the name of the database and the (only) table within that database.
The combined error messages say that the value held by originalCTag is not a number. It is most likely still an empty string since that is what you say you first assigned it to. You should check that the code that you use to assign it a valid value does what you think it does.