How to catch exception for File already in use "OleDbException (0x80004005)"

1

I have a .NET application that gets information from Access DB file, it connects to them using System.Data.OleDb.OleDbConnection, the thing is, those file are sometimes unavailable because of a .ldb file that comes and goes, when they're unavailable IIS throws me the message:

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot open the file '\SMXH001ABAP05\Database\LeanSuite.mdb'. It is already opened exclusively by another user, or you need permission to view its data.

Wich makes sense to me, the thing is I want to catch that exception so I can redirect them to a "please wait" site, but I haven't manage to catch that type of exception, and right now I don't really know where my Try Catch should be (assuming that's what I have to use).

Here is the aspx.vb file code where the connection is made.

Dim DS As System.Data.DataSet
        Dim MyCommandActual As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommandTarget As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommandFallas As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommandTool As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommandCalidad As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommandAsistencias As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommandMaterial As System.Data.OleDb.OleDbDataAdapter
        Dim MyConnection As System.Data.OleDb.OleDbConnection

        'Connecting with DB
        MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=\\SMXH001ABAP05\Database\LeanSuite.mdb")

        'Establishing Querys
        MyCommandActual = New System.Data.OleDb.OleDbDataAdapter("select * from tblSICL WHERE tblSICL.ConditionID=2628 AND StationText=" + """TAT1""", MyConnection)
            MyCommandTarget = New System.Data.OleDb.OleDbDataAdapter("select * from tblSICL WHERE tblSICL.ConditionID=2629 AND StationText=" + """TAT1""", MyConnection)
            MyCommandFallas = New System.Data.OleDb.OleDbDataAdapter("select Distinct OperationText from tblSICL, tblOperations WHERE  tblOperations.ID = tblSICL.OperationID AND tblSICL.ConditionID IN (2681,2682,2697,2698,2709,2710,2713,2714,2725,2726,2693,2694) AND Value=1 AND ZoneTypeID= 1 ", MyConnection)
            MyCommandTool = New System.Data.OleDb.OleDbDataAdapter("select Distinct OperationText from tblSICL, tblOperations WHERE tblOperations.ID = tblSICL.OperationID AND tblSICL.ConditionID in (2689,2690,2691,2692,2705,2706,2707,2708,2721,2722,2723,2724) AND Value=1 AND ZoneTypeID= 1 ", MyConnection)
            MyCommandCalidad = New System.Data.OleDb.OleDbDataAdapter("select Distinct OperationText from tblSICL, tblOperations WHERE tblOperations.ID = tblSICL.OperationID AND tblSICL.ConditionID in (2685,2686,2687,2688,2701,2702,2703,2704,2717,2718,2719,2720) AND Value=1 AND ZoneTypeID= 1 ", MyConnection)
            MyCommandAsistencias = New System.Data.OleDb.OleDbDataAdapter("select Distinct OperationText from tblSICL, tblOperations WHERE tblOperations.ID = tblSICL.OperationID AND tblSICL.ConditionID in (2679,2695,2711) AND Value=1 AND ZoneTypeID= 1 ", MyConnection)
            MyCommandMaterial = New System.Data.OleDb.OleDbDataAdapter("select Distinct OperationText from tblSICL, tblOperations WHERE tblOperations.ID = tblSICL.OperationID AND tblSICL.ConditionID in (2683,2684,2699,2716) AND Value=1 AND ZoneTypeID= 1 ", MyConnection)

            Dim fila As DataRow

            'Reading Actual Value
            DS = New System.Data.DataSet()
            MyCommandActual.Fill(DS)
            fila = DS.Tables(0).Rows(0)
            lblActual.Text = fila("Value")
            Dim objetoAndon As New AndonDBClass
            'objetoAndon.insertProduction(fila("Value"), 1)
 MyConnection.Close()
End Sub
.net
vb.net
ms-access
oledb
asked on Stack Overflow Feb 13, 2020 by EdwinRamirez45 • edited Feb 13, 2020 by xidgel

1 Answer

0

It looks like you are trying to only read the data with this connection.

See if opening the connection as read only will help here.

Change:

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=\\SMXH001ABAP05\Database\LeanSuite.mdb")

To:

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=\\SMXH001ABAP05\Database\LeanSuite.mdb; Mode=Read")

Also has others have suggest wrapping your requests in a try/catch would be helpful.

answered on Stack Overflow Feb 14, 2020 by ScottJamesSDF

User contributions licensed under CC BY-SA 3.0