?
Solved

Parameterised SQL INSERT failing with foreign characters

Posted on 2010-11-14
11
Medium Priority
?
739 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 143

Assisted Solution

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

please use adLongVarWChar, please
0
 

Author Comment

by:craig32768
ID: 34134293
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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34134325
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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

Accepted Solution

by:
craig32768 earned 0 total points
ID: 34134341
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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34134453
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
 

Author Comment

by:craig32768
ID: 34134457
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 143

Expert Comment

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

Author Comment

by:craig32768
ID: 34134842
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 800 total points
ID: 34136297
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
ID: 34140971
Cheers will try that also and see if that works/
0
 

Author Closing Comment

by:craig32768
ID: 34179115
Great feedback guys. Seems to be working now.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

764 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