Solved

Parameterised SQL INSERT failing with foreign characters

Posted on 2010-11-14
11
723 Views
Last Modified: 2012-06-22
Hi,

I've got a parameterised SQL INSERT in vbscript classic asp. It works fine with standard western characters, but when I try with say Vietnamese etc, it falls over.

I'm guessing it might be something to do with the UTF-16 / 8 chartacters causing an input to be too long?

The error I'm getting is:
Microsoft OLE DB Provider for SQL Server error '80040e21'

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

/hostform-register.asp, line 174

and the code for that section is below. For example: If I insert 19 Bi¿t Th¿ Street into HostStreet I get the error.

If I change the HostStreet from nvarchar(25) to nvarchar(50) the error does not happen on that field.... So do these values need to be higher when using foreign characters?

strSQL = "INSERT INTO HelpX_Hosts (HostID, HostEmail, HostEmail2, HostPassword1, HostPassword2, HostLastUpdated, HostDisabled, HostEmailInclude, CategoryID, HostPostCode, HostCountry, HostRegionID, HostNames, HostPropertyName, HostStreet, HostSuburb, HostCity, HostState, HostPhone, HostMobile, HostFax, HostURL, HostAllYear, HostArea, HostPersons, HostOrganicStatus, HostDetails, HostDescription, HostHeard, HostIPAddress, UserCode) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  
    Set Comm=CreateObject("ADODB.Command")
	Comm.ActiveConnection=Con
	Comm.CommandText = strSQL
  
  ' HostID INT
  Comm.Parameters.Append Comm.CreateParameter("@HostID", adInteger, adParamInput,,iNewHostID)

  ' HostEmail NVARCHAR(50)
  Comm.Parameters.Append Comm.CreateParameter("@HostEmail", adVarWChar, adParamInput,50,strHostEmail)
  ' HostEmail2 NVARCHAR(50)
  Comm.Parameters.Append Comm.CreateParameter("@HostEmail2", adVarWChar, adParamInput,50,strHostEmail)
  
  ' HostPassword1 NVARCHAR(12)
    Comm.Parameters.Append Comm.CreateParameter("@HostPassword1", adVarWChar, adParamInput,12,strHostPwd1)
  ' HostPassword2 NVARCHAR(12)
     Comm.Parameters.Append Comm.CreateParameter("@HostPassword2", adVarWChar, adParamInput,12,strHostPwd2)
	  
  ' HostLastUpdated SMALLDATETIME
  Comm.Parameters.Append Comm.CreateParameter("@HostLastUpdated", adDBTimeStamp, adParamInput, , Date())

  ' HostDisabled BIT
  Comm.Parameters.Append Comm.CreateParameter("@HostDisabled", adBoolean, adParamInput, 1, strHostDisabled)
  
  ' HostEmailInclude BIT
    Comm.Parameters.Append Comm.CreateParameter("@HostEmailInclude", adBoolean, adParamInput, 1, strHostEmailInclude)
	
  '	CategoryID INT
  Comm.Parameters.Append Comm.CreateParameter("@CategoryID", adInteger, adParamInput,,strHostCategory) 
   
  '	HostPostCode NVARCHAR(10)
	Comm.Parameters.Append Comm.CreateParameter("@HostPostCode", adVarWChar, adParamInput,10,strHostPostCode)
  
  ' HostCountry INT
  Comm.Parameters.Append Comm.CreateParameter("@HostCountry", adInteger, adParamInput,,strNetworkID) 
  
  ' HostRegionID INT
  Comm.Parameters.Append Comm.CreateParameter("@HostRegionID", adInteger, adParamInput,,strHostRegion) 
  
  ' HostNames NVARCHAR(60)
  Comm.Parameters.Append Comm.CreateParameter("@HostNames", adVarWChar, adParamInput,60,strHostNames)
  
  ' HostPropertyName NVARCHAR(60)
  Comm.Parameters.Append Comm.CreateParameter("@HostPropertyName", adVarWChar, adParamInput,60,strHostPropertyName)
  
  ' HostStreet NVARCHAR(25)
  Comm.Parameters.Append Comm.CreateParameter("@HostStreet", adVarWChar, adParamInput,100,strHostStreet)
  
  ' HostSuburb NVARCHAR(25)
  Comm.Parameters.Append Comm.CreateParameter("@HostSuburb", adVarWChar, adParamInput,25,strHostSuburb)
  
  ' HostCity NVARCHAR(25)
  Comm.Parameters.Append Comm.CreateParameter("@HostCity", adVarWChar, adParamInput,25,strHostCity)
  
  ' HostState NVARCHAR(2)
  Comm.Parameters.Append Comm.CreateParameter("@HostState", adVarWChar, adParamInput,2,strHostState)
  
  ' HostPhone NVARCHAR(25)
  Comm.Parameters.Append Comm.CreateParameter("@HostPhone", adVarWChar, adParamInput,25,strHostPhone)
  
  ' HostMobile NVARCHAR(25)
  Comm.Parameters.Append Comm.CreateParameter("@HostMobile", adVarWChar, adParamInput,25,strHostMobile)
  
  ' HostFax NVARCHAR(30)
  Comm.Parameters.Append Comm.CreateParameter("@HostFax", adVarWChar, adParamInput,25,strHostFax)
  
  ' HostURL NVARCHAR(70)
  Comm.Parameters.Append Comm.CreateParameter("@HostURL", adVarWChar, adParamInput,70,strHostURL)
  
  ' HostAllYear NVARCHAR(50)
  Comm.Parameters.Append Comm.CreateParameter("@HostAllYear", adVarWChar, adParamInput,50,strHostAllYear)
  
  ' HostArea NVARCHAR(50)
  Comm.Parameters.Append Comm.CreateParameter("@HostArea", adVarWChar, adParamInput,50,strHostArea)
  
  ' HostPersons NVARCHAR(50)
  Comm.Parameters.Append Comm.CreateParameter("@HostPersons", adVarWChar, adParamInput,50,strHostPersons)
  
  ' HostOrganicStatus NVARCHAR(100)
  Comm.Parameters.Append Comm.CreateParameter("@HostOrganicStatus", adVarWChar, adParamInput,100,strHostStatus)
  
  ' HostDetails NTEXT
  Comm.Parameters.Append Comm.CreateParameter("@HostDetails", adVarWChar, adParamInput,2147483647 ,strHostDetails)
 
  ' HostDescription NTEXT
  Comm.Parameters.Append Comm.CreateParameter("@HostDescription", adVarWChar, adParamInput, 2147483647,strHostDescription)
  
  ' HostHeard NVARCHAR(70)
  Comm.Parameters.Append Comm.CreateParameter("@HostHeard", adVarWChar, adParamInput,70,strHostHeard)
  
  ' HostIPAddress NVARCHAR(50)
  Comm.Parameters.Append Comm.CreateParameter("@HostIPAddress", adVarWChar, adParamInput,50,Request.ServerVariables("REMOTE_HOST"))
  
  ' UserCode NVARCHAR(20)
  Comm.Parameters.Append Comm.CreateParameter("@UserCode", adVarWChar, adParamInput,20,strNewUserAccountCode)
  
  Comm.Execute
  Set Comm = Nothing

Open in new window

0
Comment
Question by:craig32768
  • 6
  • 4
11 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
Comment Utility
' HostDescription NTEXT
  Comm.Parameters.Append Comm.CreateParameter("@HostDescription", adVarWChar

please use adLongVarWChar, please
0
 

Author Comment

by:craig32768
Comment Utility
So if the field is defined in the database as nvarchar(50) then the correct parameter would be:

' HostDescription NTEXT
  Comm.Parameters.Append Comm.CreateParameter("@HostDescription", adLongVarWChar, adParamInput,50,strHostStreet)

And do the same for the rest?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
no.
NTEXT is adLongVarWChar, NVARCHAR is really the adVarWChar:
http://www.w3schools.com/ADO/ado_datatypes.asp

so, I don't see what the issue would be...
especially as you put 100 for the HostStreet parameter ...
hmmmm
0
 

Accepted Solution

by:
craig32768 earned 0 total points
Comment Utility
OK. What I did was increase the nvarchar on the database table to 50 from 25 and it worked. Though the text that was sent was only 19 Bi¿t Th¿ Street (18 characters long)....

I am new to this but the number in parentheses is the maximum number of characters allowed - is that right. So nvarchar(25) should have allowed upto 25 characters?

Would the non-western characters take up 'more space'?

I don't quite get why it worked when I increased it.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
that's interesting, as indeed the size indicates should be in characters, not in bytes ...
what connection string do you use? possible some intermediate "player" is cutting  ...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:craig32768
Comment Utility
Con.Open "Provider=SQLOLEDB.1;Password=xxx;Persist Security Info=True;User ID=xxx;Initial Catalog=xxx;Data Source=xx.xx.xx.xx;Network Library=DBMSSOCN"
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
seems all nice and dandy ... must be something small that we overlook
0
 

Author Comment

by:craig32768
Comment Utility
I'll have another digg around this end. And I'll implement your adLongVarWChar suggestion also.
0
 
LVL 11

Assisted Solution

by:tobzzz
tobzzz earned 200 total points
Comment Utility
At the top of your ASP page - do you have this tag:
<%@ Language=VBscript Codepage="65001" %>
and is the file definitely saved as UTF-8? You can ensure this by opening it in Notepad and saving it again under UTF-8 format.
If there's any HTML in the page, then make sure you add this tag in the <head> too:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

Hope this helps

/ Tobzzz
0
 

Author Comment

by:craig32768
Comment Utility
Cheers will try that also and see if that works/
0
 

Author Closing Comment

by:craig32768
Comment Utility
Great feedback guys. Seems to be working now.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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

9 Experts available now in Live!

Get 1:1 Help Now