Catch exception when calling stored procedure : SqlConnection Stored Procedure : ADO.Net C# Examples


C# Examples » ADO.Net » SqlConnection Stored Procedure »

 

Catch exception when calling stored procedure









    
using  System;
using  System.Data;
using  System.Data.SqlClient;

class  MainClass
{
      static  void  Main()
      {
                  SqlConnection  conn  =  new  SqlConnection(@"data  source  =  .\sqlexpress;integrated  security  =  true;database  =  northwind");

                  SqlCommand  cmd  =  conn.CreateCommand();

                  cmd.CommandType  =  CommandType.StoredProcedure;
                  try
                  {
                        conn.Open();
                        SqlDataReader  dr  =  cmd.ExecuteReader();
                        dr.Close();
                  }
                  catch  (System.Data.SqlClient.SqlException  ex)
                  {
                        Console.WriteLine("Source:  "  +  ex.Source);
                        Console.WriteLine("Exception  Message:  "  +  ex.Message);
                  }
                  catch  (System.Exception  ex)
                  {
                        Console.WriteLine("Source:  "  +  ex.Source);
                        Console.WriteLine("Exception  Message:  "  +  ex.Message);
                  }
                  finally
                  {
                        if  (conn.State  ==  ConnectionState.Open)
                        {
                              Console.WriteLine("Finally  block  closing  the  connection");
                              conn.Close();
                        }
                  }
      }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlConnection Stored Procedure