Reputation: 17
Problem: I am currently using a vb application(Visual Studio 2012) to query my database (SQL Server 2012) to check if the row exists in a table if the row does exist then delete it. However if it does not exist then add the row to the database.
Additional information: I have text boxes on my vb application with the data. This data is saved to the database.
I have complied the code and cannot seem to find the error in the line of code. Please find the code below!
Code:
cmd2.CommandText =
"IF EXISTS(SELECT * FROM(tblLocation) WHERE Tag_ID = '"
& txttags.Text
& "') THEN (DELETE FROM tblLocation WHERE Tag_ID = '"
& txttags.Text
& ")' ELSE INSERT INTO tblLocation([Area], [Area_Time], [TagID]) VALUES('"
& txtLocation.Text & "' , '"
& txtdate.Text & "','" & txttags.Text & "')"
Any help or suggestions would be greatly appreciated!
Upvotes: 1
Views: 6546
Reputation: 6374
Remove the parenthesis before/after the DELETE statement as follows:
cmd2.CommandText =
"IF EXISTS(SELECT * FROM tblLocation WHERE Tag_ID = '"
& txttags.Text
& "') DELETE FROM tblLocation WHERE Tag_ID = '"
& txttags.Text
& "' ELSE INSERT INTO tblLocation([Area], [Area_Time], [TagID]) VALUES('"
& txtLocation.Text & "' , '"
& txtdate.Text & "','" & txttags.Text & "')"
Upvotes: 1