Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Parameterised SQL INSERT failing with foreign characters

Posted on 2010-11-14
11
Medium Priority
?
747 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

580 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