Solved

SQL Insert Error

Posted on 2011-09-30
4
185 Views
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);
                con.Open();
               SqlCommand cmd = new SqlCommand(sSQL, con);
                cmd.ExecuteNonQuery();
              
        
        
        
       
        

        //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);
         oda.Fill(odt);

Open in new window

0
Comment
Question by:olootu
[X]
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
4 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 125 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 [ ].

Lee
0
 
LVL 18

Assisted Solution

by:lludden
lludden earned 125 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)";
0
 
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.
0
 

Author Comment

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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

726 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