Solved

Parameterised SQL INSERT failing with foreign characters

Posted on 2010-11-14
11
728 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 200 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
query question 12 32
Multiple functions in SQL select statement 4 27
IIS components 2 13
In sql, how to roll up multiple rows to only one row. 4 30
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

792 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