Checking the connection state

After your application has established a connection to the database, you can check the connection state to verify that the connection is open before you fetch data from the database to update it. If a connection is lost or busy, or if another command is being processed, you can return an appropriate message.

The AseConnection class has a “state property” that checks the state of the connection. Possible state values are Open and Closed.

The following code checks whether the Connection object has been initialized, and if it has, it verifies that the connection is open.

For C#:

if( _conn == null || _conn.State != 
   ConnectionState.Open ) 
   {
      MessageBox.Show( "Connect to a database first",
      "Not connected" );
      return;
   }

For Visual Basic .NET:

If (_conn Is Nothing) OrElse (_conn.State <> ConnectionState.Open) Then
     MessageBox.Show("Connection to a database first",
     "Error")
     Return
End If

A message is returned if the connection is not open. For more information, see “State property”.