5 Replies - 563 Views - Last Post: 20 July 2016 - 10:36 AM

#1 RustyAMcM  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 192
  • Joined: 21-January 15

SQL Server 2014 insert statement problems

Posted 20 July 2016 - 08:57 AM

I have a SQL dababase with table named FeeTBL that contains a column named CallerName. I am trying to insert the value "Mary Caller"
in this field but get an error stating invalid syntax near "Caller" if I remove the space and insert "MaryCaller" it is fine. What I want to know is this : How can i modify my statement or database to allow the space in the name below is the code as it is now...

 try
            { 
            string SQLInsertQuery = "INSERT INTO FeeTBL (LastName, FirstName, CallerName, DK, StarName, CompanyName, ContactMethod, FType, FTask, RecordLocator, AgentNo, CreationDate, CreationTime, NotesComments, Fee) VALUES (" + LName + ", " + FName + ", " + CallName + ", " + DK + ", " + SName + ", " + TCName + ", " + Method + ", " + Type + ", " + Task + ", " + RecLoc + ", " + AgentNumber + ", " + Dte + ", " + Tme + ", " + Notes_Comm + ", " + FFee + ",'False', 'False', '0')";


                con.Open();
            SqlCommand cmd = new SqlCommand(SQLInsertQuery, con);
              
            cmd.ExecuteNonQuery();

                con.Close();

            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }


Is This A Good Question/Topic? 0
  • +

Replies To: SQL Server 2014 insert statement problems

#2 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2343
  • View blog
  • Posts: 9,400
  • Joined: 03-December 12

Re: SQL Server 2014 insert statement problems

Posted 20 July 2016 - 09:01 AM

Parameterized statements come to mind..

What is the column type and size?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: SQL Server 2014 insert statement problems

Posted 20 July 2016 - 09:02 AM

Simply: don't do this. Never, ever, generate SQL with string concatenation.

Instead, use Parameterized Queries: http://www.aspsnippe...es-ADO.Net.aspx

One reason why is the obligatory:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 RustyAMcM  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 192
  • Joined: 21-January 15

Re: SQL Server 2014 insert statement problems

Posted 20 July 2016 - 09:05 AM

astonecipher,

This is a char(113) field I will try parameters and see if I can work it out.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2343
  • View blog
  • Posts: 9,400
  • Joined: 03-December 12

Re: SQL Server 2014 insert statement problems

Posted 20 July 2016 - 10:28 AM

Any reason you are using char over varchar?
Was This Post Helpful? 0
  • +
  • -

#6 RustyAMcM  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 192
  • Joined: 21-January 15

Re: SQL Server 2014 insert statement problems

Posted 20 July 2016 - 10:36 AM

Ok well added the Parameters and that took care of the problem thanks for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1