WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

DbCommand against Oracle fails with String/VARCHAR parameters

Summary: When using the DbCommand object against an Oracle database fails when using String/VARCHAR parameters.
 
Keywords: DBCOMMAND
ORACLE
ORACLECLIENT
ORACLECOMMAND
STRING
VARCHAR
VARCHAR2
Applicable Software: Active Server Pages .NET (ASP.NET)
ADO
C#
Visual Basic .NET (VB.NET)
 
Body:

I've found that using Oracle from ASP.NET to be a great experience.... except when things go wrong. Then, suddenly - I find that I'm in some kind of "no-man's land" where other people may or may not have experienced the same thing as me. But, usually it was more than a year ago, for a different version of .NET and no real solutions. I'm going to try and start documenting my trials and tribulations in this knowledgebase, because that's what it is here for.

For this particular issue, when I had a DbCommand object that was created with an OracleClientFactory - I was getting a failure, ORA-12571: TNS:packet writer failure whenever using string procedure parameters. The code that illustrates this error is:

Dim cmd As DbCommand = Me.Factory.CreateCommand()       '** this is a valid OracleFactory object
Dim param As DbParameter = Nothing
Dim sql As String = ""

sql = "SELECT ard.*" & Environment.NewLine & _
       "FROM MyTable ard" & Environment.NewLine & _
       "WHERE ard.Client_Nbr = :ClientNbr" & Environment.NewLine

       param = Me.Factory.CreateParameter()
       param.DbType = DbType.String
       param.Direction = ParameterDirection.Input
       param.ParameterName = "Client_Nbr"
       param.Value = "Yaba"

       cmd.Parameters.Add(param)

       cmd.Connection = Me.Connection  '** this is a valid DbConnection to an Oracle database

       cmd.CommandText = sql

       dr = cmd.ExecuteReader()

To fix this issue, I had to use the DbType.AnsiString instead of DbType.String:

Dim cmd As DbCommand = Me.Factory.CreateCommand()       '** this is a valid OracleFactory object
Dim param As DbParameter = Nothing
Dim sql As String = ""

sql = "SELECT ard.*" & Environment.NewLine & _
       "FROM MyTable ard" & Environment.NewLine & _
       "WHERE ard.Client_Nbr = :ClientNbr" & Environment.NewLine

       param = Me.Factory.CreateParameter()
       param.DbType = DbType.AnsiString
       param.Direction = ParameterDirection.Input
       param.ParameterName = "Client_Nbr"
       param.Value = "Yaba"

       cmd.Parameters.Add(param)

       cmd.Connection = Me.Connection  '** this is a valid DbConnection to an Oracle database

       cmd.CommandText = sql

       dr = cmd.ExecuteReader()

Sadly, I don't necessarily know why this works or what reprecussions may come of this. But, it works...

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 4/25/2006 12:04:00 PM

Rate this article: Average: 10
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.