0
SqlConnection con1 = new SqlConnection(strcon);
con1.Open();

string query = " ";
query += " BEGIN TRANSACTION ";
query += " DELETE FROM VehicleRentals FROM VehicleRentals INNER JOIN Vehicles N VehicleRentals.VehicleID = Vehicles.VehicleID WHERE LicensePlate=@LicensePlate ";
query += " DECLARE @x int ";
query += " SELECT @x = VehicleTypeCode FROM Vehicles WHERE LicensePlate=@LicensePlate ";
query += " DELETE FROM Manufacturers FROM Manufacturers INNER JOIN Models ON Manufacturers.ManufacturerCode = Models.ManufacturerCode INNER JOIN Vehicles ON Models.ModelID = Vehicles.ModelID WHERE LicensePlate=@LicensePlate ";
query += " DELETE FROM VehicleTypes FROM VehicleTypes WHERE VehicleTypeCode = @x ";
query += " COMMIT TRANSACTION ";

SqlCommand cmd1 = new SqlCommand(query, con1);
cmd1.Parameters.AddWithValue("@LicensePlate", txtPlaka.Text);
cmd1.ExecuteNonQuery();
con1.Close();

I fixed the code as you said. Spaces between lines and I'm using parameter @LicensePlate. But the code is not working

5
  • 7
    You don have spaces between the statements. You can use a verbatim string literal to avoid writing query += for every new line. You should also use sql-paremeters instead of string concatenation to prevent sql-injection. Commented May 23, 2014 at 15:09
  • Look at query in the debugger and you'll see the problem. Commented May 23, 2014 at 15:09
  • 1
    Curious what will happen when you enter this license plate number? Try it out! Commented May 23, 2014 at 15:14
  • This question makes no sense. Did there use to be a question here that got edited out? Commented May 23, 2014 at 15:43
  • @AndrewS - Yea OP edited and is now asking about problems after the edit.. Commented May 23, 2014 at 15:44

1 Answer 1

5
query += "BEGIN TRANSACTION";
query += "DELETE FROM VehicleRentals FROM VehicleRentals INNER JOIN Vehicles N..."

turns into

"BEGIN TRANSACTIONDELETE FROM..."

You need to include the spaces between each line:

query += "BEGIN TRANSACTION";
query += " DELETE FROM VehicleRentals FROM VehicleRentals INNER JOIN Vehicles N..."

And as others have stated, you should use parameters to avoid sql injection.

Sign up to request clarification or add additional context in comments.

7 Comments

Plus, as tim schmeter has said, you need to use variables too.
True. I added a note about it but usually don't include it in my answer when the question is about the query not executing properly.
I fixed as you said. Using spaces between lines and parameter. But still the code is not working. I edit the question above
The second line of your query has typos. You repeat FROM VehicleRentals twice and you have N instead of ON in your join. Not sure if there's other syntax problems; I didn't look through the entire query so check that everything is spelled correctly. Also, you don't need spaces at the beginning and end of each line (but it doesn't hurt anything). You just need at least one space in between each word.
I appreaciate for your answer. It is working now. Thank you very much!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.