• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2977
  • Last Modified:

C# Program does not throw exception

Env: C#, .NET3.5, Oracle provider OPT.NET v11.2 Rel 3

This is a simple C# program to open an Oracle database and count the number of records in one dataset. If the program throws an exception it just silently ends with no type of error message. That is the issue - why is it not throwing an exception?
Example: There is a typo in the TNS Oracle db name wich will cause an exception on the db open command. If i use a try/catch, it detects the eror and I can display it, but if I dont use a try/catch, the program justs ends with no error message. How do I force it to display a stack trace or exception without using a try/catch. I do assume this has something to do with the oracle provider .NET interface OPT.NET behavior.

Code:
// compile:
// csc prog1.cs /r:D:\app\oracle\product\11.2.0\client_1\odp.net\bin\2.x\Oracle.DataAccess.dll
using System;
using System.Data.Common;
using Oracle.DataAccess.Client;
class OPTNetTest
{
      static void Main(string[] args)
{
// Create database connection object
Console.WriteLine("Begin execution - example using Oracle Client provider.");
DbProviderFactory oraFactory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");
DbConnection objDb = oraFactory.CreateConnection();
objDb.ConnectionString = "Data Source = myOraDB; User Id=user1; Password=xxx";
Console.WriteLine("Opening database.");
objDb.Open();
OracleCommand SQLCmd = (oracleCommand)oraFactory.CreateCommand();
SQLCmd.Connection = (OracleConnection)objDb;
SQLCmd.CommandText = "SELECT COUNT(*) FROM myTable";
int recCtr = Convert.ToInt32(SQLCmd.ExecuteScalar());
Console.WriteLine("Rows=" + recCtr);
objDb.Close();
objDb.Dispose();
      }            //end-main
}            //end-class
0
STYX757
Asked:
STYX757
1 Solution
 
AshokCommented:
You need to use

try...catch

HTH
Ashok
0
 
slightwv (䄆 Netminder) Commented:
I get the error.

I had to tweak you code a little to correct a case issue on oracleCommand

My code:
// compile:
// csc prog1.cs /r:D:\app\oracle\product\11.2.0\client_1\odp.net\bin\2.x\Oracle.DataAccess.dll
using System;
using System.Data.Common;
using Oracle.DataAccess.Client;

class OPTNetTest
{
      static void Main(string[] args)
{
// Create database connection object
Console.WriteLine("Begin execution - example using Oracle Client provider.");
DbProviderFactory oraFactory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");
DbConnection objDb = oraFactory.CreateConnection();
objDb.ConnectionString = "Data Source = myOraDB; User Id=user1; Password=xxx";
Console.WriteLine("Opening database.");
objDb.Open();
OracleCommand SQLCmd = (OracleCommand)oraFactory.CreateCommand();
SQLCmd.Connection = (OracleConnection)objDb;
SQLCmd.CommandText = "SELECT COUNT(*) FROM myTable";
int recCtr = Convert.ToInt32(SQLCmd.ExecuteScalar());
Console.WriteLine("Rows=" + recCtr);
objDb.Close();
objDb.Dispose();
      }            //end-main
}            //end-class 

Open in new window


My config file:
<configuration>
  <system.data>
    <DbProviderFactories>
<add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </DbProviderFactories>
  </system.data>
</configuration>

Open in new window


My bat compile line:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc /platform:x86 /r:System.dll /r:System.Web.dll /r:System.Xml.dll /r:System.Data.dll /r:Oracle.DataAccess.dll /t:library /t:exe prog1.cs

Open in new window


My output:
U:\console_apps>prog1.exe
Begin execution - example using Oracle Client provider.
Opening database.

Unhandled Exception: Oracle.DataAccess.Client.OracleException: ORA-12154: TNS:co
uld not resolve the connect identifier specified
   at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode,
OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src
, String procedure, Boolean bCheck)
   at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, Oracle
Connection conn, IntPtr opsErrCtx, Object src)
   at Oracle.DataAccess.Client.OracleConnection.Open()
   at OPTNetTest.Main(String[] args)

Open in new window

0
 
STYX757Author Commented:
Thanks for info! yes, I realize I can surround try/catch on critical statements but I do not want to do this for every code statement.

So why do you get the stack trace? was it because you used a config file? I am not using one.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
slightwv (䄆 Netminder) Commented:
>>So why do you get the stack trace? was it because you used a config file? I am not using one.

Not sure.  The setup I used used an ODP.Net Instant client trick where I copied the 5 necessary files locally into the same folder, console_apps in this case.

It might also have to do with the ODP and/or .Net version.

Can you reproduce my results copying my setup?

The contents of my console_apps folder are:
oci.dll
Oracle.DataAccess.dll
orannzsbb11.dll
oraociei11.dll
OraOps11w.dll
prog1.bat
prog1.cs
prog1.exe
prog1.exe.config
0
 
mvdeveloperCommented:
Exceptions will propagate upward through the stack, so you don't have to wrap every statement - just the top of your invocation tree.

If you want a single point to capture these wrap the Application.Run() in your program.cs.
0
 
STYX757Author Commented:
I will move the 5 critical files into the prog folder and re-compile it. Now why did you create the prog1.config file - is it required for the DbProviderFactory Class? - cause i do have this program working fine - as long as there is no exception thrown and I'm not using a prog1.config file.
0
 
slightwv (䄆 Netminder) Commented:
>>Now why did you create the prog1.config file - is it required for the DbProviderFactory Class?

It was for the DbProviderFactory.  If I didn't have it, it gave a different exception when it tried to load the factory.

I've done a fair amount with .Net and ODP.Net and have never used DbProviderFactory so I did what Google told me to do...  create the config file with that entry.

>>and I'm not using a prog1.config file

I'm also not on a complete web server and don't have Visual Studio (VS) installed.  Just a desktop machine with the .Net framework installed.

It is likely VS is taking care of this for you and it likely registered things that I did not.
0
 
STYX757Author Commented:
ah, ok. Well I have some interesting news.
On my server (WIN2008), i switched the compile to C# .NET4 and used the following compile string:
csc prog1.cs /r:D:\app\oracle\product\11.2.0\client_1\odp.net\bin\4\Oracle.DataAccess.dll
The program compiled ok and now works ok and throws exceptions with a stacktrace.
So then I proceeded to re-compile with C# .net4 on my XP-PRO laptop.
When I run the program on my laptop:
D:>prog1

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass
embly 'Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyToken=89b
483f429c47342' or one of its dependencies. The system cannot find the file speci
fied.
   at OPTNetTest.Main(String[] args)

Anyone have any ideas on why it works ok with .net4 on my server and gives this error on my laptop?
Thanks!
0
 
slightwv (䄆 Netminder) Commented:
Do you have Oracle.DataAccess.dll in the same folder and is it the same version (4.112.2.0)?
0
 
STYX757Author Commented:
The version in my provider is 4.112.2.0:
D:\app\oracle\product\11.2.0\client_1\odp.net\bin\4
I don't have a copy of the dll in my run folder since when i ran it on my win2008 I didn't have a copy of it in the run folder and the program ran fine.
However, I did copy it over to my run folder on my laptop and got something different:
D:>prog1
Begin execution - example using Oracle Client provider.

Unhandled Exception: System.ArgumentException: Unable to find the requested .Net
 Framework Data Provider.  It may not be installed.
   at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariant
Name)
   at OPTNetTest.Main(String[] args)
0
 
slightwv (䄆 Netminder) Commented:
>> at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariant
Name)

This is the error my config file fixes.

I'm far from a .Net expert so have no clue what needs to be set up differently between your laptop and server.  My guess is it has something to do with the GAC and IIS.
0
 
STYX757Author Commented:
OK, I have resolved the issue on my XP Pro laptop. When I installed .NET4 Framework, it corrupted the installation of the Oracle ODAC. The Oracle provider were missing from the GAC .
C:\windows\Microsoft.NET\assembly\GAC_32
You should see folders:
Oracle.DataAccess
Oracle.Web
Policy.4.112.Oracle.DataAccess
Policy.4.112.Oracle.Web
So this error:
Unhandled Exception: System.ArgumentException: Unable to find the requested .Net
 Framework Data Provider.  It may not be installed.
   at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariant
Name)
   at OPTNetTest.Main(String[] args)

means that the provider was not found in the GAC.
The solution was to de-install the ODAC and re-install it.

As for the matter of c# .NET3.5 not reporting runtime errors and stacktraces, this was resolved by migrating the program to .NET4 and the ver 4 of the Oracle provider:
csc prog1.cs /r:D:\app\oracle\product\11.2.0\client_1\odp.net\bin\4\Oracle.DataAccess.dll
0
 
slightwv (䄆 Netminder) Commented:
You could have probably just reregisterd the DLL with the GAC but glad it is now working.
0
 
STYX757Author Commented:
Resolution was posted by person who posted question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now