Any characters or string not accepted in MS SQL?

Posted on 2011-03-09
Last Modified: 2012-05-11
I have a text (an article in fact) that i want to insert into a column (text). I noticed that with the HTML tag (even with HTMLencode), the text will not be inserted to MS SQL.

However, with just a normal text (with all the HTML tag removed), it's able to be inserted.

Thus my question is that any char, string, text format that is not accepted in MS SQL? So that i can filter out these unacceptable combination prior to inserting.

Question by:tangteng78
  • 4
  • 4
  • 2
LVL 23

Expert Comment

ID: 35091110
No, SQL Server accepts all characters.  

The issue should be on the code that is inserting the data.  Can you post the code in question?

Author Comment

ID: 35091932
Same code is used.

I'd attached to you the 2 different input, one is the HTML text (which is not being inserted) vs the normal text (which work just fine).

LVL 23

Expert Comment

ID: 35092312
1-are you using any particular programming to insert the HTML? if you are using ASP, you  have to set ValidateRequest=false on the top of the page_Directive.

2-you can also create a procedure to handle the html codes as shown in another thread:

Author Comment

ID: 35094307
What's the difference then? Coz the same code is able to insert normal text. I'm using (vb). Still confuse if this is due to the coding.
LVL 23

Expert Comment

ID: 35094408
Please post the code that inserts the data.
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


Author Comment

ID: 35103213
There you go.

function that takes in 2 parameters (the strBody variable is the one that takes in the HTML/normal text)

objDb.insert(strSubject, HttpUtility.HtmlEncode(strBody))

And the class objDB class itself.
    'Insert new tweets
    Function insert(ByVal strTitle As String, ByVal strBody As String) As Integer

        'Setup and open connection string
        Dim con As New SqlConnection("Data Source=database/table,3181;DATABASE=;User ID=db;Password=db12;Trusted_Connection=False")


        'Setup dataset and data adapter
        Dim dt As New DataTable
        Dim adp As New SqlDataAdapter

        'Setup stored procedure calls
        Dim cmd As New SqlCommand("isure", con)
        With cmd
            .CommandType = CommandType.StoredProcedure
            .Parameters.AddWithValue("@action", "insert")
            .Parameters("@action").Direction = ParameterDirection.Input
            .Parameters.AddWithValue("@id", String.Empty)
            .Parameters("@id").Direction = ParameterDirection.Input
            .Parameters.AddWithValue("@title", strTitle)
            .Parameters("@title").Direction = ParameterDirection.Input
            .Parameters.AddWithValue("@body", strBody)
            .Parameters("@body").Direction = ParameterDirection.Input
        End With

        'Run query
        adp.SelectCommand = cmd

        'Close connection

        'Return the last inserted isure_info_id
        Return CInt(dt(0)(0))

    End Function

And the store procedure.
ALTER PROCEDURE [dbo].[isure]

      @action AS VARCHAR(50),
      @id AS INTEGER,
      @title AS VARCHAR(max),
      @body as nvarchar(max)

DECLARE @NewId varchar(30)

IF @action = 'insert'
INSERT m_isure_info (title, body, created_date) VALUES (@title, @body, GETDATE())
SET @NewId=SCOPE_IDENTITY()            /*Get last inserted id*/

LVL 23

Expert Comment

ID: 35103514
Your stored proc and ASP.NET look good.  Can you verify that strBody contains a value just before calling objDb.insert?  Also, you said that the HTML value is not inserted. What happens exactly?  Does the code throws an exception, inserts nulls, or else?

Author Comment

ID: 35103877
Yes, the strBody does contain the value prior to the store procedure.And it doesn't throw any error, it seems that it inserted a null.
LVL 23

Expert Comment

ID: 35104858
What tool do you use to query the database to verify that the data is not there?

Please run the following select.

     select title, len(body), created_date, body from m_isure_info

I'm suspecting that your db tool cannot display the html data due to its size.
LVL 23

Accepted Solution

OP_Zaharin earned 500 total points
ID: 35135340
have you try other datatype than nvarchar? try ntext maybe?

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Troubleshooting Methodology - steps 3 36
testing sql16 on win10 vs OS16 2 36
SQL Server merge records in one table 2 12
Extract string portion 2 14
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

16 Experts available now in Live!

Get 1:1 Help Now