Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have problem is that. I want to insert data on MS Access Table but it gives Syntax error in INSERT INTO statement.

my code is this


C#
int pcid = int.Parse(drpProduct.SelectedValue);
            string qury = "Insert into Product_Information(PCId,ImagePath,Names,Code) values(" + pcid + ",'" + uploadPath + "','" + txtImageName.Text + "','" + txtImageCode.Text + "') ";
           
            OleDbCommand oleDbCommand = new OleDbCommand(qury, oleDbConnection);           
            if (oleDbConnection.State != ConnectionState.Open)
                oleDbConnection.Open();
            int i = oleDbCommand.ExecuteNonQuery();
            if (i > 0)
            {
                txtImageCode.Text = txtImageName.Text = string.Empty;
            }
Posted
Comments
Thomas ktg 13-Sep-13 5:53am    
Did you check manually executing the insert statement with values?
BulletVictim 13-Sep-13 5:55am    
Try writing your insert statement like this:
"Insert into [Product_Information]([PCId],[ImagePath],[Names],[Code]) values('" + pcid + "','" + uploadPath + "','" + txtImageName.Text + "','" + txtImageCode.Text + "') ";

I know the [ ] are not required, but I have seen it solve this issue before.
Dharmendra-18 13-Sep-13 6:00am    
thanks it work, but I have question Why we write this type before it all query works but only this through exception....
I have tried to explain the problem. If you like it, then please accept and up-vote.

Thanks,
Tadit
Please add this as an answer. :)

1 solution

@BulletVictim is correct.

This issue happens when you mention any reserved keyword of a particular Database.

Refer - Access 2007 reserved words and symbols[^].
Here all the keywords are listed.
From your query, "Names" is also present in the list.

So, when you use it, it will refer to the keyword and not the column of Table.
To resolve this, we wrap it inside the Square Brackets, so that it will not refer to the default keyword.
 
Share this answer
 
Comments
BulletVictim 16-Sep-13 4:18am    
Thank you. I have asked around the office as well but since we don't really work with access databases no one could think of a reason why this would happen. But what you said makes sense.
Thanks Tadit
Hi BulletVictim,

Most Welcome. My Pleasure...

Please up-vote, if it helped you. :)

Thanks,
Tadit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900