jdurman
jdurman

Reputation: 13

C# - OleDbConnection.Open() causing a crash

Fairly new to C#, anyway, I have this Initialise method that I've written, it basically creates a connection to a MS2007 Access Database, fills a DataSet with the 4 DataTables that are the result of some queries.

    public frmDBCompareForm()
    {
        ///
        /// Required for Windows Form Design support
        ///
        InitializeComponent();
        frmDBCompareForm_Initialize();

        //
        // TODO: Add any constructor code
        //
        if (_InstancePtr == null) _InstancePtr = this;
    }

And the start of the Initialise Method, including one of the DataTables being filled:

private void frmDBCompareForm_Initialize()
    {
        // Fill DataSet with 3 DataTables, these tables will be 
        // made up of the from sQuery.
        try
        {
            // Create a new DataSet
            DataSet dsSite1 = new DataSet();
            // Set up the connection strings to HCAlias.accdb
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\HCAlias.accdb;Persist Security Info=False;";
            con.Open();
            //
            // Table 1 - dtSite1Name [cmbSite1]
            //
            dtSite1Name = new DataTable();
            string sQuery = "SELECT SourceName From Sites";
            OleDbCommand cmdSite1Name = new OleDbCommand(sQuery, con);
            OleDbDataAdapter myDASite1Name = new OleDbDataAdapter(cmdSite1Name);
            myDASite1Name.Fill(dsSite1, "dtSite1Name");
            cmbSite1.DataSource = dtSite1Name;
            cmbSite2.DataSource = dtSite1Name;

Could anyone point me in the right direction for going about this the way I have? Any tips or advice to get that connection issue fixed? I've been Googling like a boss, but can't seem to find the exact issue that I'm having.

Upvotes: 1

Views: 4324

Answers (2)

BizApps
BizApps

Reputation: 6130

You also need to close your connection . Add also on your finally block with:

        using (var con = new OleDbConnection())
        {
            con.Open();
            using (var cmd = new OleDbCommand("sqlquery", conn))
            {

                try
                {
                             //do Stuff here
                }
                catch (OleDbException)
                {

                    throw;
                }

            }
         }

regards

Upvotes: 3

CRice
CRice

Reputation: 12567

This error is caused by leaving connections open. It won't necessarily happen right away but always after the same number of requests.

I suggest wrapping your IDisposable db classes with using statements:

using (OleDbConnection con = new OleDbConnection())
{

}

This will automatically call the implementation on the Dispose() method and close your connections.

From MSDN : "The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler." ... so you do not need your try catch finally

Upvotes: 2

Related Questions