SQL Insert Error

Posted on 2011-09-30
Medium Priority
Last Modified: 2012-05-12
Could anyone please tell me what is wrong with this insert query? I keep getting the error (with the bolded highlited in red):

Incorrect syntax near '`'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '`'.

Source Error:

Line 69:                 con.Open();
Line 70:                SqlCommand cmd = new SqlCommand(sSQL, con);
Line 71:                 cmd.ExecuteNonQuery();
Line 72:              
Line 73:        

string ConnStr = "Data Source=sql01.ad.gov.uk\\spur;Initial Catalog=PCN_Payments;User ID=spppppppp;Password=passsss ";
                string sSQL = " INSERT INTO capitapayment(fundcode,fundname,ref,amt,source,rurl,backurl,vatcode,custemail,reconscript,sysref,`when`,`live`) VALUES ('31','Car Penalty Charge Notice','" + pcn.ToString() + "','" + amt + " ','I ',' www.ad.gov.uk ',' " + backurl + "',' F ','','','" + pcn.ToString() + "|" + vrm.ToString() + "',NOW(), 1)";
                SqlConnection con = new SqlConnection(ConnStr);
               SqlCommand cmd = new SqlCommand(sSQL, con);

        //Get the last inserted ID
        string lastID = " SELECT @@IDENTITY as id";
      SqlCommand IDcmd = new SqlCommand(lastID, con);
        DataTable odt = new DataTable();
      SqlDataAdapter oda = new SqlDataAdapter(IDcmd);

Open in new window

Question by:olootu
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
  • 2
LVL 25

Accepted Solution

Lee Savidge earned 500 total points
ID: 36891519
Looks like you're using back ticks ` instead of single quotes '

If these are reserved words being used as column names surround with [ ].

LVL 18

Assisted Solution

lludden earned 500 total points
ID: 36891522
The items I bolded.  You can't have a string constant in the columns list/

string sSQL = " INSERT INTO capitapayment(fundcode,fundname,ref,amt,source,rurl,backurl,vatcode,custemail,reconscript,sysref,`when`,`live`) VALUES ('31','Car Penalty Charge Notice','" + pcn.ToString() + "','" + amt + " ','I ',' www.ad.gov.uk ',' " + backurl + "',' F ','','','" + pcn.ToString() + "|" + vrm.ToString() + "',NOW(), 1)";
LVL 25

Expert Comment

by:Lee Savidge
ID: 36891527
I hope that connection string isn't for a live db with the user name and password in it.

Author Comment

ID: 36891918
@lsavidge: The connectionstring is NOT correct. I just left a dummy logindetails.

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

762 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