tom
tom

Reputation: 5001

Checking for an open oracle connection

im trying to write a simple gui form to display if any database connections my app needs are unavilable

    public void tryConnection(List<String> connections, List<String> databaseNames)
    {
        tableLayoutPanel1.RowCount = connections.Count();
        Label label;
        Label sucOrFail;
        String message = "";

        int row = 0;
        for (int i = 0; i < connections.Count(); i++ )
        {
            try
            {
                label = new Label();
                sucOrFail = new Label();
                label.AutoSize = true;
                sucOrFail.AutoSize = true;
                label.Text = databaseNames[i].ToUpper();
                tableLayoutPanel1.Controls.Add(label, 0, row);
                OracleConnection con = new OracleConnection(connections[i]);
                con.Open();

                message = "Success, You Managed to connect to " + databaseNames[i];
                sucOrFail.Text = message;
                sucOrFail.ForeColor = System.Drawing.Color.LawnGreen;
                tableLayoutPanel1.Controls.Add(sucOrFail, 1, row);
                con.Close();
                row++;
            }
            catch (OracleException e)
            {
                Console.WriteLine("failure");
            }
        }

is there anyway i can amend this so that if con.open() fails it doesnt skip in to the catch, because when this happens i loose my place in the loop and cant continue as i would need to.

For example

if (con.open())
{
    message = ......
}
else
{
    message = .....
}

Upvotes: 1

Views: 1698

Answers (2)

Alain Pannetier
Alain Pannetier

Reputation: 9514

You should have dedicated try catch block around the con.Open().

Upvotes: 0

dcp
dcp

Reputation: 55467

Why not just use an inner try/catch inside the loop:

 for (int i = 0; i < connections.Count(); i++ )
 {
    try {
      con.Open();
      // connection ok
    }
    catch (OracleException e) {
      // couldn't connect
    }
 }

Upvotes: 1

Related Questions