none
TextBox Does Not take Single Quote(') RRS feed

  • Question

  • Hello Freinds i Have a little problem.

    When i Insert a Single Quote(') in textbox then it does not take it gives me erroe "Incorrect Syntax near "

    Suppose i insert "charls " in textbox then it insert in database but if i insert "charle's" in textbox then it gives me that error.

    please help Me.

    Thanks in Advance 


    • Edited by k k k Wednesday, March 6, 2013 9:04 AM
    Wednesday, March 6, 2013 5:57 AM

Answers

  • Are you storing value in TextBox to database. You can store ' directly by using insert query. For example, in below query, in SomeStreet there is "'" between Some and street and it will fail in SQL with error "Incorrect syntax near"

    INSERT INTO [QQQ].[dbo].[Employee]
               ([Id]
               ,[Name]
               ,[Add])
         VALUES
               ( '1' ,'Adam','Some'Street')
    GO

    How to fix it:

    One simple way is use ' to escape '. see below

    INSERT INTO [QQQ].[dbo].[Employee]
               ([Id]
               ,[Name]
               ,[Add])
         VALUES
               ( '1' ,'Adam','Some''a')
    GO

    Best way is to use SQLParameter class and use parameter binding. This will take care all values:

    SqlParameter Class

    Hope it helps 

    Cheers

    VV

    Wednesday, March 6, 2013 6:08 AM