[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# Program does not throw exception

Posted on 2012-08-15
14
Medium Priority
?
2,865 Views
Last Modified: 2012-08-25
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
Comment
Question by:STYX757
14 Comments
 
LVL 13

Expert Comment

by:Ashok
ID: 38296404
You need to use

try...catch

HTH
Ashok
0
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38296487
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
 

Author Comment

by:STYX757
ID: 38296557
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38296579
>>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
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 38296600
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
 

Author Comment

by:STYX757
ID: 38296770
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38296863
>>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
 

Author Comment

by:STYX757
ID: 38297794
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38297809
Do you have Oracle.DataAccess.dll in the same folder and is it the same version (4.112.2.0)?
0
 

Author Comment

by:STYX757
ID: 38297986
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38298112
>> 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
 

Accepted Solution

by:
STYX757 earned 0 total points
ID: 38300406
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38300414
You could have probably just reregisterd the DLL with the GAC but glad it is now working.
0
 

Author Closing Comment

by:STYX757
ID: 38332090
Resolution was posted by person who posted question.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
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
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

872 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