Hi guys... I have a radio button that one checked will connect to an ADO Connection.

The following code being used is:

     private void radioButton3_CheckedChanged(object sender, EventArgs e)
            //Fields Disabled until a new Account is created.//
            txtAccRef.Enabled = false;
            txtAccName.Enabled = false;
            txtAccAddr1.Enabled = false;
            txtAccAddr2.Enabled = false;
            txtAccTown.Enabled = false;
            txtAccCounty.Enabled = false;
            txtAccPostCode.Enabled = false;

            //Buttons Disabled as not needed//
            btnNewCashAcc.Enabled = false;
            btnEditCashAcc.Enabled = false;
            btnSaveCashAcc.Enabled = false;
            btnDeleteCashAcc.Enabled = false;

            //As Data is being pulled from Line 50 this fields have cleared.//

            //Opens the Line 50 Connection//
            adoConn.Open("SageLine50v19", "Manager", "", 0);

            //Creates new versions of the Connection string and Data Set//
            da = new OleDbDataAdapter();
            ds4 = new DataSet();

What is best method of using an IF statement to check if this particular connection even exists on a machine?


Recommended Answers

All 4 Replies

I'm guessing you're using ODBC?

1) Manually check the registry for the appropriate key. You will need to check System wide keys and Account keys [HKLM/HKCU]/Software/ODBC/ODBC.INI/<ConnectionKey>

2) Try/Catch the adoConn.Open for and see what exception you get back when the connection doesn't exist (Should be OdbcException)

Hello again Ketsueiame,

There is no <connectionKey> and i'm currently on a 64Bit version running ODBC so checked there also.

I'm using a 'Sage 50' Driver to connect to the Sage 50 DB so looking for a IF statement to check if this Driver Exisits then Run the code IF not then MessageBox.Show

The <connectionkey> means it's a placeholder for your connection key.

No idea how Sage works, but if it does use ODBC then simply try and open it in a try/catch block and catch that message. If the "Open" method throws an exception you can't continue anyway.

Hi Ketsuekiame,

Many thanks for your help..

I used a Try/Catch to pickup the errors in question. This seems to have resolved the problem I wanted.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.