I'm creating a web form to insert information into a database. I had no issue getting this set up to insert information that was typed, but I ran into trouble trying to reformat the command to UPDATE information if the entry already exists in the database.
I'm getting the following error when I try to submit the changes:
There was an error parsing the query. [ Token line number = 1,Token line offset = 7,Token in error = Assets ]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlServerCe.SqlCeException: There was an error parsing the query. [ Token line number = 1,Token line offset = 7,Token in error = Assets ]
Source Error:
Line 39: INSERT (AssetNumber, SerialNumber, AssetType, Model, Location, DateModified, Comments), Line 40: VALUES (@0, @1, @2, @3, @4, @5, @6)"; Line 41: db.Execute(insertCommand, assetNumber, serialNumber, assetType, model, toCube, currentTime, comments); Line 42: Response.Redirect("~/AssetLookup"); Line 43: }
The code errors out at line 41. The only thing I changed in the code below is the SQL statement, so I'm sure there's something wrong with that. This is my first time trying to work with SQL; I did some digging around trying to figure out what is wrong with the statement, but in particular I'm having trouble with the USING ... ON part. I'm not really sure WHAT should go there, and I feel like that's probably the issue.
var assetNumber = "";
var serialNumber = "";
var assetType = "";
var model = "";
var toCube = "";
var comments = "";
if(IsPost && Validation.IsValid()){
assetNumber = Request.Form["assetNumber"];
serialNumber = Request.Form["serialNumber"];
assetType = Request.Form["assetType"];
model = Request.Form["model"];
toCube = Request.Form["toCube"];
var currentTime = DateTime.Now;
comments = Request.Form["comments"];
var db = Database.Open("Assets");
var insertCommand =
@"MERGE Assets AS Target
USING (SELECT assetNumber, serialNumber FROM Assets) AS Source
ON (Target.assetNumber = Source.assetNumber AND Target.serialNumber = Source.serialNumber)
WHEN MATCHED THEN
UPDATE SET
AssetNumber=@0,
SerialNumber=@1,
AssetType=@2,
Model=@3,
Location=@4,
DateModified=@5,
Comments=@6
WHEN NOT MATCHED THEN
INSERT (AssetNumber, SerialNumber, AssetType, Model, Location, DateModified, Comments),
VALUES (@0, @1, @2, @3, @4, @5, @6)";
db.Execute(insertCommand, assetNumber, serialNumber, assetType, model, toCube, currentTime, comments);
Response.Redirect("~/AssetLookup");
}
Like I said, I'm sure my SQL statement is messed up somewhere as I "kind of" understand it but am particularly confused on the USING...ON part. All table columns and variable names are set properly per the rest of the code/db. Can someone please offer some advice on the syntax I should use for this command?
UPDATE:
Based on Steve's comment, I have updated the question to reflect that this is SQL CE and not the standard version.