locked
Issue saving single quotes to database RRS feed

  • Question

  • User-2136750558 posted

    I'm using .net 4.0 to insert textbox values into a sql database.  Whenever I try to insert single quotes into the database, the values are appearing as &#39.  How do I save single quotes to the database without having the value converted to #39?   

    Wednesday, March 28, 2018 1:45 AM

All replies

  • User-369506445 posted

    hi

    please try below code :

       string sql = "insert into yourTable (text) " +
                "values ('" + textBox.Text.Replace("'", "''") + "')";

    Wednesday, March 28, 2018 5:46 AM
  • User-2136750558 posted

    Hi,

    I've updated the code below but &#39 is still being saved to the database.

         Public Sub AddMessage(ByVal TransactID As String, ByVal Message As String)
                If Not String.IsNullOrEmpty(Note) Then
                    Message.Replace("'", "''")
                    MessageHandler.InsertMessage(TransactID, Message)
                End If
    
            End Sub

    Wednesday, March 28, 2018 2:00 PM
  • User753101303 posted

    Hi,

    String methods are returning a new string so at best it would be :

    Message=Message.Replace("'","''")

    If it doesn't help, you could check the value found in Message using the debugger :
    - if you see already &#39 then it seems the string was encoded at some point but stayed encoded (maybe it was encoded twice so when posted back and decoded you still have this HTML entity in your string)
    - if not it happens later and could depend on your database. ? (so having this kind of encoding option looks weird. Maybe an Oracle option we don't have with SQL Server ?)

    Wednesday, March 28, 2018 2:51 PM