I have a program to communicate with database. One of the functions is to INSERT data into the database, which is giving me a syntax error!
I can't figure out why...
"INSERT INTO Report ('Date', 'Reporter', 'Details') VALUES ('27/1/2010', 'Mark Smith, 11260(S)', 'gfdsgsdfgsdf')"
I also tried it like this:
"INSERT INTO Report (Date, Reporter, Details) VALUES ('27/1/2010', 'Mark Smith, 11260(S)', 'gfdsgsdfgsdf')"
Can someone please help?
Andrew
theLizardPosted Mar 28, 2010, 7:03 AM
string command = "INSERT INTO Report (Date, Reporter, Details) VALUES ('" + DateText.Text + "', '" + ReporterText.Text + "', '" + DetailsText.Text + "')";
Questions? what database engine are you using.
What are the values being parsed
What is the value of command on a break point.
If there are no problems with the values and you are not reserved words, I am not sure if Date is, if it is you may need to bracket like [Date] otherwise there is no reason why you would get an error from what I see.
AndrewPosted Mar 29, 2010, 5:04 AM
I didn't use integers as ID, since the ID includes (s) or an other letter.
Hope I didn't confuse you ;)
theLizardPosted Mar 29, 2010, 4:39 AM
I do not use Access so I don't know for sure how access works with ID but I would have expected that id's are integers what does (S) represent!
Is ID your identifier or an auto increment, just curious. I may also learn something new today... :)
AndrewPosted Mar 29, 2010, 4:22 AM
theLizardPosted Mar 29, 2010, 4:16 AM
Sometimes you need to read things more than once, this is one of those times, generally ID's are integers so I think where you say WHERE ID = '" + impId + "';" is your problem
can you see anything wrong? you have single quotes either side of your impId, these indicate a string data type, remove the single quotes and your statement should work.
AndrewPosted Mar 29, 2010, 4:03 AM
theLizardPosted Mar 28, 2010, 5:03 PM
at first glance it looks ok but I think Time may also be one of those words, I tend to begin my vars in lower case this normally overcomes these types of issues, date is not the same as Date so sql server does not complain.
string MyString = "UPDATE Md SET Name='" + NText.Text + "',A/D (not sure what / would do) ='" + DText.Text + "',[Time]='" + TText.Text +"' WHERE ID='" + ImpId + "';";
other examples of how I name vars is Customer Name -> customerName, Supplier Name -> supplierName
AndrewPosted Mar 28, 2010, 7:43 AM
What are the values being parsed . : All values are of type TEXT
AndrewPosted Mar 28, 2010, 5:08 AM
Syntax error in INSERT INTO statement." :(
theLizardPosted Mar 27, 2010, 11:45 PM
string command = "INSERT INTO Report " + "(Date, Reporter, Details) " + "VALUES ('" + DateText.Text + "', '" + ReporterText.Text + "', '" DetailsText.Text.Replace("'", "''") + "')";
do this
AndrewPosted Mar 27, 2010, 8:20 AM
VasanthPosted Mar 27, 2010, 7:59 AM
If varchar means your second query is right.
But if [date] datatype is datetime means , please cast datetime as like sqlserver format. Please run the below query.
INSERT INTO Report (Date, Reporter, Details) VALUES ('1/27/2010', 'Mark Smith, 11260(S)', 'gfdsgsdfgsdf')
Just i changed the date column value. If its not helps you please revert table schema also and error text.
If it helps you , please mark as answer.
Amit ChoudharyPosted Mar 27, 2010, 7:25 AM
your second one is right with syntax but i have dobut i your query
"INSERT INTO Report (Date, Reporter, Details) VALUES ('27/1/2010', 'Mark Smith,1260(S)', 'gfdsgsdfgsdf')"
use escape sequence before inserting special characters in your data using insert statement.
Please mark as answer if it helps.