Insert into Access Database
Hey all, I've been trying to Insert into my access database for some days now and I'm still getting a syntax error which I have no idea why, my connection works I was able to delete buy I cannot add for some reason
14 Replies
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code! (see $code for more information on how to paste your code)
Also, don't use Access for database
@Mike B how do you expect us to diagnose your syntax error without the code in question?
how do i format the code
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/But sql instead of cs
(If it’s just the sql statement)
this is the info i got from my form and the generated inserts statement
1234, 'Porsche', '911', 2024, 23, 89.99, 'Fast', 'Lot B', 'Available', 'Sport', 'porsche.png'
INSERT into Cars(VIN, Make, Model, CarYear, Mileage, RentalCost, Description, Location, Availability, Class, Image) VALUES (1234, 'Porsche', '911', 2024, 23, 89.99, 'Fast', 'Lot B', 'Available', 'Sport', 'porsche.png')
Exception thrown: 'System.Data.OleDb.OleDbException' in System.Data.dll
First, you need to switch to parameterized queries.
String interpolation in SQL is dangerous, it’s how you get SQL injection.
Parameterized queries also free you from dealing with headaches around escaping.
One sec let me update the code, I tried it and still have gotten issues
updated and I still recieved the same error
this is my table
this is my car class
I believe you need unique names for the parameters when you Add.
Or use the single-arg variant, if one exists.
If you continue having trouble, I would suggest switching to #database