DekGenius.com
[ Team LiB ] Previous Section Next Section

18.4 Events Reference

InfoMessage

{Sql |OleDb}InfoMessageEventHandler InfoMessage

Fires when a warning or informational messages is sent from a data source. If the message corresponds to an error, an exception is raised instead of this event being fired. Information about the message is stored in a provider-specific EventArgs object, such as OleDbInfoMessageEventArgs or SqlInfoMessageEventArgs.

This event isn't used to receive specific error messages (in SQL Server, these are messages with a severity greater than 10) that will cause an exception to be thrown.

Event Argument

{Sql|OleDb}InfoMessageEventArgs e

The InfoMessageEventArgs object is provider-specific. Typically, it includes information such as the error number and message text, as well as the additional provider-specific information. For example, SQL Server provides information about the database, stored procedure, and line number from which the message originated.

Example

Here's an example event handler for the InfoMessage event. It displays some of the retrieved information in a console window:

private void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)
{
    foreach (SqlError err in args.Errors)
    {
        Console.WriteLine("The {0} has received a severity {1}, " +
         "state {2} error number {3} on line {4} of procedure {5} " +
         "on server {6}", err.Source, err.Class, err.State, err.Number,
         err.LineNumber, err.Procedure, err.Server);
    }
}
StateChange

StateChangeEventHandler StateChange

Fires when the connection is opened or closed. This event provides a StateChangeEventArgs object with information about the current and previous state as a value from the System.Data.ConnectionState enumeration.

Event Argument

StateChangeEventArgs e

Provides two properties. CurrentState indicates the current state of the connection (which triggered the StateChange event), and OriginalState indicates the state of the Connection object before the event was fired.

Example

Here's an example method that handles the StateChange event and displays some information to a console window:

private void OnStateChange(object sender, StateChangeEventArgs e)
{
    Console.WriteLine("Original State = " + e.OriginalState);
    Console.WriteLine("Current State = " + e.CurrentState);
}
    [ Team LiB ] Previous Section Next Section