CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Error

  1. #1
    Join Date
    Dec 2007
    Posts
    7

    Error

    SQLSERVER2000
    ----------------------

    Dear All,


    I want to update to a table field. I use
    Query= 'Select 'c' distinct S.Name'.

    i want to enter a varchar value like that having 'c' inside but sqlserver give me an error.

    Server: Msg 170, Level 15, State 1, Line 1
    Line 1: Incorrect syntax near 'c'.



    update AllQueryFields set Query= 'Select 'c' distinct S.Name '
    Where ObjectName ='0303' and FieldAlias='Status'


    how i can enter that value

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Error

    Don't put the column name in quotes. Just use it as:

    SELECT c FROM [table]

  3. #3
    Join Date
    Dec 2007
    Posts
    7

    Re: Error

    For example
    ----------------
    update AllQueryFields set t = replace('you can ''c'' me','''','`')
    Where ObjectName ='0303' and FieldAlias='Status'

    this code is working but if a user must enter two times single quote then enter one time single quote in the database.

    for example if a user enter in a textbox
    you can 'c' me ..... he must be type

    you can ''c'' me

    then single quote enter in the database.

    Any suggestion for correct my code????

    thanks

  4. #4
    Join Date
    Aug 2005
    Location
    Seattle, Wa
    Posts
    179

    Re: Error

    Don't expect the user to fix the issue, when the user types an singlequotes into a field that goes into a databse, you need to replace the singlequote with two singlequotes.

    For example: a user enters this into a textbox...
    Code:
    I want to enter 'this' into the database
    You need to replace the singlequotes with two singlequotes before you place it into your sql command.

    Code:
    string sqlQuery = "update sometable set somefield = '" + textBox1.Text.Replace("'", "''") + "'";

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured