A developer at work queried me to why RaiseError generates an exception when working with a DataSet but not when working with an IDataReader. Here is a short code fragement that demonstrates the problem.

<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /> /o:p

using System;/o:p

using System.Data;/o:p

using System.Data.SqlClient;/o:p

 /o:p

class TestRaiseError/o:p

{/o:p

      static void <?xml:namespace prefix = st1 ns = “urn:schemas-microsoft-com:office:smarttags” />st1:placeMain/st1:place(string[] args)/o:p

      {/o:p

            SqlConnection connection = new SqlConnection(“server=myServer;database=northwind;uid=sa;pwd=password”);/o:p

            SqlCommand command = new SqlCommand(“select * from Employees RAISERROR (‘raise an error’, 16, 1)“, connection);/o:p

            IDataReader reader = null;/o:p

            try/o:p

            {/o:p

                  connection.Open();/o:p

                  reader = command.ExecuteReader();/o:p

                  while (reader.Read())/o:p

                  {/o:p

                        Console.WriteLine(reader.GetString(1));/o:p

                  }/o:p

            }/o:p

            finally/o:p

            {/o:p

                  if (reader != null)/o:p

                  {/o:p

                        reader.Close();/o:p

                  }/o:p

                  if (connection != null)/o:p

                  {/o:p

                        connection.Close();/o:p

                  }/o:p

            }           /o:p

      }/o:p

}/o:p

 /o:p

If you compile and run the code fragment you’ll see no exception is thrown yet the sql statement being executed contains a raise error. If the program was rewritten to use a DataSet and a SqlDataAdpater then an exception would be thrown.

 /o:p

This behaviour may seem a little strange but it is by design. It is very important to remember that the an IDataReader represents an active connection to the database and records from the database will be retrieved in turn where as a dataset grabs all results from a query and places them in the clients memory.

 /o:p

It is possible to return a number of different results sets from a stored procedure, when using an IDataReader these are accessed using .NextResult(). Interestingly the ADO.NET client treats RaiseError as separate results set and it is this that can lead to slightly surprising results. This means that if you have a stored procedure something like:

 /o:p

SELECT * FROM myTable

 /o:p

RAISERROR (‘raise an error’, 16, 1)

 /o:p

Then it is possible to read all the contents of myTable with out ever seeing the exception because you need to call .NextResult() to see the exception that is raised.

 /o:p

As the DbDataAdapter that fills the DataSet is written to enumerate over all results sets  from the query so the exception from the RaiseError is picked up.

 /o:p

I’ve create a small winfoms application which demonstrates all the situations described in here. It is available here./o:p