Publish date: 2003-04-23

OLE DB Exceptions Handling (C#, VB.Net)

Download the example archive
 using System;
using System.Data;
using System.Data.OleDb;

namespace Error_Collection
{
    /// <summary>
    /// Sample Ole Db ErrorColection (IErrorRecords interface)
    /// </summary>
    class ErrorColection
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {    
            OleDbConnection con; 

            try
            {    
                //Create new Connection
                Console.WriteLine ("Create new connection");
                con =new OleDbConnection("Provider=LCPI.IBProvider;");
                Console.WriteLine("Trying open connection");
                con.Open();
                
                Console.WriteLine("Close connection if connection state is open");        
                if (con.State  == ConnectionState.Open)
                   {con.Close();}
            }    
            catch(OleDbException myOLEDBException)
            { 
                //ole db exception
                //error collection sample 
                Console.WriteLine("----------------------------------------");
                    for (int i = 0; i<=myOLEDBException.Errors.Count-1;i++)
                    {
                    Console.WriteLine("Message " + (i + 1) + ": " + myOLEDBException.Errors[i].Message);
                    Console.WriteLine("Native: " + myOLEDBException.Errors[i].NativeError.ToString());
                    Console.WriteLine("Source: " + myOLEDBException.Errors[i].Source);
                    Console.WriteLine("SQL: " + myOLEDBException.Errors[i].SQLState);
                    Console.WriteLine("----------------------------------------");
                    }
            }
        catch (Exception myException)
        {
            //other exception
            //error collection sample
            Console.WriteLine("----------------------------------------");
            Console.WriteLine(myException.Message);
        }
          
        Console.WriteLine("All Done. Press Enter to close");
        Console.ReadLine();

        }
    }
}
Download the example archive
Output of sample.
Output.



Download the example archive
 Imports System.Data.OleDb

'OLE DB ErrorCollection sample
Module ErrorCollection

    Public Sub Main()

        Dim i As Integer
        Dim con As OleDbConnection

        Try
            'Create new Connection
            Console.WriteLine("Create new connection")
            con = New OleDbConnection("Provider=LCPI.IBProvider;")

            Console.WriteLine("Trying open connection")
            con.Open()

        Catch myOLEDBException As OleDbException 'ole db exception
            'error collection sample 
            Console.WriteLine("----------------------------------------")
            For i = 0 To myOLEDBException.Errors.Count - 1
                Console.WriteLine("Message " & i.ToString + 1 & ": " + myOLEDBException.Errors(i).Message)
                Console.WriteLine("Native: " + myOLEDBException.Errors(i).NativeError.ToString())
                Console.WriteLine("Source: " + myOLEDBException.Errors(i).Source)
                Console.WriteLine("SQL: " + myOLEDBException.Errors(i).SQLState)
                Console.WriteLine("----------------------------------------")
            Next i

        Catch myException As Exception  'other exception
            'error collection sample
            Console.WriteLine("----------------------------------------")
            Console.WriteLine(myException.Message)
        End Try

        Console.WriteLine("Close connection if connection state is open")
        If con.State = ConnectionState.Open Then
            con.Close()
        End If

        Console.WriteLine("All Done. Press Enter to close")
        Console.ReadLine()

    End Sub

End Module
Download the example archive
Output of sample.
Output.