ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples


Handling Connection Events
Previous Home Next

Both the OLE DB and the SQL Server Connection objects provide two events:

  1. StateChange
  2. InfoMessage.
  1. StateChange: The StateChange event fires whenever the state of the Connection object changes. The event passes a StateChangeEventArgs to its handler, which, inturn, has two properties: OriginalState and CurrentState. The possible values for

  2. OriginalState and CurrentState are:

    Connection States Meaning
    Broken TheConnecton is open, but not functional. It maybe closed and reopened
    Closed The Connection is closed
    Connecting The Connection is in the process of connecting, but has not yet been
    Executing Executing the command
    Fetching Retrieving the data
    Open Open the connection

    To display the previous and current Connection states for each of the two Connection objects: Select OleDbConnection1 in the Class Name combobox of the editor and the StateChange event in the Method Name combobox.

    private void oleDbConnection1_StateChange
       (object sender,StateChangeEventArgs e)
    string s;
    s = "The Connection State is changing from " 
                  + e.OriginalState.ToString() +
             " to " + e.CurrentState.ToString();
    private void SqlDbConnection1_StateChange 
      (object sender, StateChangeEventArgs e)
    string s1;
    s1 = "The Connection State is changing from " +
                     e.OriginalState.ToString() +
                 " to " + e.CurrentState.ToString();

    Add the code to connect the event handlers to the:

    ConnectionProperties sub:

    this.oleDbConnection1.StateChange +=
    this.SqlDbConnection1.StateChange += 
       new System.Data.StateChangeEventHandler

    Save and run the program. Change the Connection Type and then click the Test button.

    The application displays two MessageBoxes as the Connection is opened and closed.

  3. InfoMessage: The InfoMessage event is triggered when the data source returns warnings. The information passed to the event handler depends on the Data Provider.

Previous Home Next