Solved

SQL Insert Error

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

813 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

11 Experts available now in Live!

Get 1:1 Help Now