UniException: catch specific exception

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for universal data access
Post Reply
mlagasio
Posts: 43
Joined: Mon 14 Mar 2011 13:42

UniException: catch specific exception

Post by mlagasio » Tue 22 Mar 2011 10:02

We are migrating our ms vc# application from use only SqlServer db to use SqlServer and Oracle db.

We are using your products.

In few cases we test SqlException.Error to catch a specific error.
Now using UniException we have not this chance.

How could we test for a specific exception that is provider specific?

Many thanks in advance

Bye
Marco

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Wed 23 Mar 2011 13:54

Please try the following code:

Code: Select all

    string mySelectQuery = "some SQL";
    using (UniConnection uniConnection = new UniConnection()){
        uniConnection.ConnectionString = "Provider=SQL Server;Data Source=YourDS;Initial Catalog=YourIC;User ID=YourUID;";
        UniCommand uniCommand = new UniCommand(mySelectQuery, uniConnection);
        try {
            uniCommand.Connection.Open();
            uniCommand.ExecuteNonQuery();
        }
        catch (UniException uniException) {
            Type exType = uniException.InnerException.GetType();
            switch (exType.FullName) {
                case "System.Data.SqlClient.SqlException":
                    System.Data.SqlClient.SqlException sqlException = (SqlException)uniException.InnerException;
                    for (int i = 0; i < sqlException.Errors.Count; i++) {
                        Console.WriteLine("Index #" + i + "\n" +
                            "Error: " + sqlException.Errors[i].ToString() + "\n");
                    }
                    break;
                default:
                    Console.WriteLine("Message: " + uniException.Message + "\n");
                    break;
            }
        } 
    }
Is that what you mean?

mlagasio
Posts: 43
Joined: Mon 14 Mar 2011 13:42

Post by mlagasio » Wed 23 Mar 2011 14:38

Yes, but adding some rows into the catch:

Type exType = ex.InnerException.GetType();
switch (exType.FullName)
{
case "System.Data.SqlClient.SqlException":
System.Data.SqlClient.SqlException sqlException = (System.Data.SqlClient.SqlException)ex.InnerException;
for (int i = 0; i < sqlException.Errors.Count; i++)
{
Console.WriteLine("Index #" + i + "\n" +
"Error: " + sqlException.Errors.ToString() + "\n");
}
break;
case "Devart.Data.Oracle.OracleException":
Devart.Data.Oracle.OracleException oraException = (Devart.Data.Oracle.OracleException)ex.InnerException;
for (int i = 0; i < oraException.Errors.Count; i++)
{
Console.WriteLine("Index #" + i + "\n" +
"Error: " + oraException.Errors.ToString() + "\n");
}
break;
default:
Console.WriteLine("Message: " + ex.Message + "\n");
break;
}


I've this error

"Unable to cast object of type 'Devart.Data.Oracle.OracleException' to type 'Devart.Data.Oracle.OracleException'."

in the case of Provider=Oracle in connection string (error on case

case "Devart.Data.Oracle.OracleException":

during cast).

Thanks

Marco

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Wed 23 Mar 2011 17:14

Devart.Data.Oracle.OracleException is not available as a public class in the assemblies of dotConnect Universal. This is a designed behaviour, because the provider specific classes should be available only when this particular provider is installed, and its assemblies are referenced in your project.

We will investigate and notify you about the results concerning these 2 issues:
1) we will consider the possibility of implementing the Devart.Data.Universal.UniException.Errors property with the corresponding collection;
2) the reason of the "Unable to cast object of type 'Devart.Data.Oracle.OracleException' to type 'Devart.Data.Oracle.OracleException'." error. It occurs if the project has a reference to Devart.Data.Oracle.dll from dotConnect for Oracle (e.g., free dotConnect for Oracle Express). As I understood, you were using this scenario in the code from your previous post.

mlagasio
Posts: 43
Joined: Mon 14 Mar 2011 13:42

Post by mlagasio » Thu 24 Mar 2011 08:24

Hi. Yes my project has a reference to Devart.Data.Oracle.dll from dotConnect for Oracle (not the free version). As you say (if I've understood) then provider specific classes are available...
(It runs when I connect to Sql Server, the cast has no problem).

I remain in standby, waiting your investigation results.

Many thanks
Marco Lagasio

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Thu 24 Mar 2011 17:14

1. The Errors collection (the "multi error" feature) is not available in all servers which can be accessed via dotConnect Universal. So, there is no Devart.Data.Universal.UniException.Errors.

2. dotConnect Universal does not work with Express Editions of other dotConnects (e.g., dotConnect for Oracle Express) - as designed. But you can use other editions of dotConnect for Oracle for this case: Standard, Professional, and Developer.
If your edition of dotConnect Universal is Professional, you should implement the following trick to make it work with Devart.Data.Oracle.dll. There are two assemblies Devart.Data.Universal.Oracle.dll provided with the installation package:
a) \Program Files\Devart\dotConnect\Universal\Providers\Devart.Data.Universal.Oracle.dll is used WITHOUT Devart.Data.Oracle.dll because the last is merged with the first during building;
b) \Program Files\Devart\dotConnect\Universal\Providers\External\Devart.Data.Universal.Oracle.dll is used WITH Devart.Data.Oracle.dll because the first is only a "bridge" between dotConnect Universal and Devart.Data.Oracle.dll.
We are interested in the assembly b) for our scenario. The assembly a) is added to GAC during installation. So, please replace a) with b) in GAC manually. Then add references to Devart.Data.dll, Devart.Data.Universal.dll, Devart.Data.Universal.Oracle.dll (b), and Devart.Data.Oracle.dll (not from Express) to your project and try the code. It should work.

3. Devart.Data.Universal.Oracle.dll (a) will contain a public Devart.Data.Oracle.OracleException class starting from the next buld of dotConnect Universal. So, there will be no need for Devart.Data.Oracle.dll if your edition of dotConnect Universal is Professional.

mlagasio
Posts: 43
Joined: Mon 14 Mar 2011 13:42

Post by mlagasio » Fri 25 Mar 2011 10:49

Hi,

thank you for the answer. Some feedbacks.

1) all servers have not also an error code and a common subset of error category?

2) now all work (your answer has been resolutive)

3) good

Bye, Marco

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Mon 28 Mar 2011 08:51

mlagasio wrote:1) all servers have not also an error code and a common subset of error category?
Please refer to the UniException class members:
a) there is the UniException.ErrorCode property;
b) give us an example of "a common subset of error category" (the names of the corresponding properties which are available in the exception class of other ADO.NET providers but not available in UniException).

mlagasio
Posts: 43
Joined: Mon 14 Mar 2011 13:42

Post by mlagasio » Mon 04 Apr 2011 09:02

Hi,

when I say

"a common subset of error category"

I mean

primary key violation
foreign key violation
.....

I think all rdb have these concepts, so I would think possible have a property in UniException that give a unified enumerated for these errors (the property ErrorCode is probably different).
Anayway your previous answer have given to me hints to create the enumerated by myself.

Many thanks
Marco

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Wed 06 Apr 2011 11:02

mlagasio wrote:"a common subset of error category"

I mean

primary key violation
foreign key violation
.....
Could you please tell us the names of the corresponding property(-ies) of the XXXException class in dotConnect for Oracle or .NET Framework Data Provider for SQL Server (System.Data.SqlClient) as an example of what you mean?

As we mentioned, the Errors collection (the "multi error" feature) is not available in all servers which can be accessed via dotConnect Universal. So, there is no Devart.Data.Universal.UniException.Errors.

mlagasio
Posts: 43
Joined: Mon 14 Mar 2011 13:42

Post by mlagasio » Wed 06 Apr 2011 13:46

Hi,

I refer to properties

Code in Devart.Data.Oracle.OracleException
Number in System.Data.SqlClient.SqlException

The names and values are obviously different but in some case the mean of the errors are the same (i.e. Primary Key Violation

Code=1;
Number=2657;

)

Bye, Marco

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Thu 07 Apr 2011 12:25

mlagasio wrote:I refer to properties

Code in Devart.Data.Oracle.OracleException
Number in System.Data.SqlClient.SqlException
We will investigate such possibility and notify you about the results.

New build of dotConnect Universal 3.20.46 is available for download now!
It can be downloaded from http://www.devart.com/dotconnect/univer ... nload.html (trial version) or from Registered Users' Area (for users with valid subscription only): http://secure.devart.com/ .
For more information, please refer to http://www.devart.com/forums/viewtopic.php?t=20712 .

Post Reply