error in my SQL syntax

Posted on 2011-03-16
Last Modified: 2012-05-11
Hi Guys,

I am at a loss if i run the following statement in MySQLworkbench it works fine. However when I try using the ODBC connection in c# i get the following error;

ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.56-community]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT @@IDENTITY' at line 1

the SQL i am using is very simple;

connection = new OdbcConnection(connectionString);

            String oQRY = String.Format("INSERT INTO `message` (fromID,subject,message,date,important) VALUES ({0},'{1}','{2}','{3}',{4}); SELECT @@IDENTITY AS `identity`;", userID, SubjectTextBox.Text, MessageMemo.Text, timestamp, ImportantCheckBox.Value);
            //Response.Write(oQry + "<BR>")
            OdbcCommand com = new OdbcCommand(oQRY, connection);
            com.CommandText = "SELECT Last_Insert_ID()";
            int messageID = (int) com.ExecuteScalar();

the value of oQRY is

"INSERT INTO `message` (fromID,subject,message,date,important) VALUES (0,'test','test','2011-03-16 00:00:00',True); SELECT @@IDENTITY AS `identity`;"

any ideas whats going on here please?

Thanks in advance,

Question by:flynny
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

enachemc earned 500 total points
ID: 35145787
@@IDENTITY is for MS SQL only
you are using mysql
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 35147037
Replace with this query

            String oQRY = String.Format("INSERT INTO `message` (fromID,subject,message,date,important) VALUES (SELECT @@IDENTITY AS `identity`,'{0}','{1}','{2}',{3});", userID, SubjectTextBox.Text, MessageMemo.Text, timestamp, ImportantCheckBox.Value);

Hope this helps

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question