Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parameterised SQL INSERT failing with foreign characters

Posted on 2010-11-14
11
Medium Priority
?
743 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

604 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