Solved

SQL Insert Error

Posted on 2011-09-30
4
179 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
  • 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now