10 The Sybase Data Provider : Error Handling

Error Handling
The SybaseError object collects information relevant to errors and warnings generated by the Sybase server. See “SybaseError Class” for more information.
The SybaseException object is created and thrown when the Sybase server returns an error. Exceptions generated by the data provider are returned as standard run-time exceptions. If an exception is raised by an error from the data source, it is returned as a provider-specific exception.
If the exception is raised by the data provider itself, it is returned as a standard .NET Framework exception. Some provider-specific code is required when handling provider-specific exceptions.
See “SybaseException Class” for more information.
Handling Raiserror Statements
The data provider returns errors generated by raiserror statements by throwing a SybaseException.
For example, suppose you have the following stored procedure and user-defined error:
sp_addmessage 20100,'This is a sample raiserror'
 
create procedure raiserrortest as
begin
select 1
raiserror 20100
end
The raiserror statement throws an exception in the application.
SybaseConnection Conn;
Conn = new SybaseConnection("Host=bowhead;Port=4100;User ID=test01;
Password=test01");
 
SybaseCommand DBCmd = new SybaseCommand("raiserrortest", Conn);
SybaseDataReader myDataReader;
try
{
   Conn.Open();
   DBCmd.CommandType = CommandType.StoredProcedure;
   myDataReader = DBCmd.ExecuteReader();
   while (myDataReader.Read())
   {
      // Just keep reading all the records till finished.
   }
   // Now skip to the next result set.
   // This will throw an exception as the raiserrortest
   // stored procedure generates an error at this point.
   myDataReader.NextResult();
}
catch (Exception ex)
{
   // Display the exception in a messagebox
   MessageBox.Show (ex.Message);
}
// Close the connection
Conn.Close();
Handling Print Statements
The Sybase data provider returns all Print statement errors as database warnings. The data provider initiates the SybaseInfoMessageEvent when a Print statement is encountered, as shown in the following code fragment:
// Define an event handler
public void myHandler(object sender, SybaseInfoMessageEventArgs e)
{
   // Display any warnings in a messagebox
   MessageBox.Show (e.Message,"This is a Print Statement");
}
Add the following code fragment to a method, and call that method:
SybaseConnection   Conn;
Conn = new SybaseConnection("Host=bowhead;Port=4100;User ID=test01;
   Password=test01");
 
SybaseCommand DBCmd = new SybaseCommand("printtest", Conn);
SybaseDataReader    myDataReader;
try
{
   // Add the user's event handler to the InfoMessage delegate
   Conn.InfoMessage = new SybaseInfoMessageEventHandler(myHandler);
   Conn.Open();
   DBCmd.CommandType = CommandType.StoredProcedure;
   myDataReader = DBCmd.ExecuteReader();
   while (myDataReader.Read())
   {
      // Just keep reading all the records until finished.
   }
   // Now skip to the next result set.
   // This will throw a SybaseInfoMessageEvent as the printtest
   // stored procedure generates a warning at this point.
   myDataReader.NextResult();
}
catch (Exception ex)
{
   // Display any exceptions in a messagebox
   MessageBox.Show (ex.Message);
}
// Close the connection
Conn.Close();