We help IT Professionals succeed at work.
Get Started

C# Program does not throw exception

STYX757
STYX757 asked
on
3,143 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
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE