Thanks, I've tried the following already within INSERT query, but it didn't work for some reason:
'" & cast(replace(cast(oldMessa
Not sure what's wrong with it, any idea?
Main Topics
Browse All TopicsMy Replace() string function in he query below for objRecordSet("Message") just wouldn't work on SQL Server 2005. This recordset has unicode character string NTEXT. I have tried using the following if-else-statement before running the query, but my objRecordSet("Message") just stores an empty string.
dim oldMessage, newMessage
oldMessage = objRecordSet("Message")
if newMessage = "null" then
newMessage = objRecordSet("Message")
end if
dbConn.Execute("Insert Into Files (Title, SectionId, UserId, Description, Comments, CheckOut, MajorVersion, MinorVersion, URL, DLCId, CreateDate, Message, IsDeleted, BaseFileId, PreviousFileId, CheckOutDate, CurrentFile, CreatorId, LastPopBy, intLinks, extLinks, iLinkCount, eLinkCount) values ('" & Replace(objRecordSet("Titl
Please, please advise?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I prefer not to change the column data type. So, back to the same problem, how can add this to INSERT query:
'" & cast(replace(cast(Message as varchar(MAX)),"'", "''") as ntext) & "'
I have tried the following, but it didn't work:
dbConn.Execute("Insert Into Files (Title, SectionId, UserId, Description, Comments, CheckOut, MajorVersion, MinorVersion, URL, DLCId, CreateDate, Message, IsDeleted, BaseFileId, PreviousFileId, CheckOutDate, CurrentFile, CreatorId, LastPopBy, intLinks, extLinks, iLinkCount, eLinkCount) values ('" & Replace(objRecordSet("Titl
Try this:
I put the replace inside of the string you are executing, so that it uses the SQL replace, and not the vbs.
But I would STRONGLY encourage you to consider using a stored procedure with parameterized in put for your insert and not inline SQL.
Because it could be bad if someone entered the following into your field where cmtis being pulled from:
';drop table files;--
That's a SQL injection attack at it's simplest form.
No it doesn't work.
I've also tried the following but without any luck:
dim oldMessage, newMessage
oldMessage = objRecordSet("Message")
newMessage = replace(cast(" &oldMessage & "as varchar(MAX)),""'"", ""''"")
dbConn.Execute("Insert Into Files (Title, SectionId, UserId, Description, Comments, CheckOut, MajorVersion, MinorVersion, URL, DLCId, CreateDate, Message, IsDeleted, BaseFileId, PreviousFileId, CheckOutDate, CurrentFile, CreatorId, LastPopBy, intLinks, extLinks, iLinkCount, eLinkCount) values ('" & Replace(objRecordSet("Titl
Here's what you need to determine. Is it a problem with the SQL replace, or the asp code.
To test, change your insert into files, to insert JUST the nTEXT value (with the replacements) into two different columns in a DUMMY test table. Just create a table with ntext and nvarchar(max). Try changing your execute to insert into that table.
Or, just do a response.write and try writing the unmodified value out to your page somewhere to see if the value is being retrieved correctly.
All of these steps will help you to identify what ISN'T working about this.
I've changed database column datatype to nvarchar(max) and tried the following:
Replace(objRecordSet("Mess
Replace(cast(objRecordSet(
and even this:
dim oldMessage, newMessage
oldMessage = objRecordSet("Message")
newMessage = replace(cast(" &oldMessage & "as varchar(MAX)),""'"", ""''"")
dbConn.Execute("Insert Into Files (Title, SectionId, UserId, Description, Comments, CheckOut, MajorVersion, MinorVersion, URL, DLCId, CreateDate, Message, IsDeleted, BaseFileId, PreviousFileId, CheckOutDate, CurrentFile, CreatorId, LastPopBy, intLinks, extLinks, iLinkCount, eLinkCount) values ('" & Replace(objRecordSet("Titl
But nothing, any other ideas?
Thanks.
I did try your suggestion!
First I've created dummy table called CheckFiles with the following recordsets Message (nvarchar(MAX)) and MessageOld (ntext). Then I tried to retrieve their values as follows:
If Recordset.EOF Then
Response.Write("No records returned.")
Else
Do While NOT Recordset.Eof
Response.write Recordset("Message")
Response.write "<br>--------------<br>"
Response.write Recordset("MessageOld")
Response.write "<br>"
Recordset.MoveNext
Loop
End If
The recordset Message should have displayed the same contents as MessageOld but it didn't. This was the result:
-------------
jhjjghghg
Second, I've tried to test this query
dbConn.Execute("Insert Into CheckFiles (Message, MessageOld) values ('" & Replace(objRecordSet("Mess
But nothing happen????? Really confused now...
Also I've just tried the following tests:
'----------------------1--
dbConn.Execute("Insert Into CheckFiles (Message, MessageOld) values ('" & Replace(objRecordSet("Mess
and received this error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][SQL Native Client][SQL Server]Unclosed quotation mark after the character string ''.
'----------------------2--
dim oldMessage, newMessage
newMessage = objRecordSet("Message")
oldMessage = objRecordSet("MessageOld")
dbConn.Execute("Insert Into CheckFiles (Message, MessageOld) values ('" & Replace(objRecordSet("Mess
and received this error:
Microsoft VBScript runtime error '800a005e'
Invalid use of Null: 'Replace'
'-------------------------
So objRecordSet is opened using SQL Native client, which is populated with an nvarchar(max) and ntext field. You then are attempting to write them back to the database, after doing some sort of string replacement?
Mind sharing your connection string? Make sure you remove any usernaame/password and mask your server name. Also, maybe take a look at this to ensure you are setting up your connection string correctly.
http://msdn.microsoft.com/
Ok I have some results when I've changed the following connection:
dbConn.Open "Driver={SQL Native Client};Server=servername;
to:
dbConn.Open "Provider=SQLNCLI;Server=s
I was able to retreive the Message nvarchar(max) value:
If Recordset.EOF Then
Response.Write("No records returned.")
Else
Do While NOT Recordset.Eof
Response.write Recordset("Message")
Response.write "<br>--------------<br>"
Response.write Recordset("MessageOld")
Response.write "<br>"
Recordset.MoveNext
Loop
End If
Now we are back to the same problem I've had from the begining:
dbConn.Execute("Insert Into CheckFiles (Message, MessageOld) values ('" & Replace(objRecordSet("Mess
Any Ideas?
Right, it works now. I've changed my recordset Message datatype from ntext to nvarchar(max) in SQL 2005 database and used the Replace() function as it was:
'" & Replace(objRecordSet("Mess
The connection provider was also a problem, so Ive changed it throughout my page as follows:
FROM : dbConn.Open "Driver={SQL Native Client};Server=servername;
TO: dbConn.Open "Provider=SQLNCLI;Server=s
Brandon, thank you ever so much for all your pointers and help!
Svjetlana
Business Accounts
Answer for Membership
by: BrandonGalderisiPosted on 2008-09-10 at 17:58:35ID: 22444766
If you need to do a replace on an nText Field, you need to cast it as nvarchar(max)
Select allOpen in new window