title: Try Catch Finally - dispose of a reader
body: <p>In C#, I am using a <code>TRY..CATCH..FINALLY</code> block in my code surrounding the executing of a stored procedure.</p>
  
  <p>If there is an exception, then in my <code>FINALLY</code> I want to close the reader - <code>PolicyResult</code> as well as the connections. </p>
  
  <p>However, I get an error</p>
  
  <blockquote>
    <p><em>The name PolicyResult does not exist in the current context</em></p>
  </blockquote>
  
  <p><code>PolicyResult</code> is the DataReader defined in the <code>TRY</code> but it seems to not be recognized in the <code>FINALLY</code> section. </p>
  
  <p>Why?</p>
  
  <pre><code>        public static IEnumerable GetPolicies(int? MasterPkgID)
          {
              // Create a list of policies belonging to the master package.
              List&lt;AdditionalInterestPolicyData&gt; additionalInterestPolicyData = new List&lt;AdditionalInterestPolicyData&gt;();
  
              // Set the SQL connection to the database.
              SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["QUESTIONNAIRE2"].ConnectionString);
  
              try
              {
                 // Open the connection.
                 objConn.Open();
  
                 // Get the list of policies by executing a stored procedure.
                 SqlCommand PolicyCmd = new SqlCommand("p_expapp_get_policy_detail_by_master_pkg", objConn);
                 PolicyCmd.Parameters.Clear();
                 PolicyCmd.CommandType = CommandType.StoredProcedure;
                 PolicyCmd.Parameters.Add("@a_master_package_iden_key", SqlDbType.Int).Value = MasterPkgID;
  
                 SqlDataReader PolicyResult = PolicyCmd.ExecuteReader();
  
                 // Loop thru the results returned.
                 while (PolicyResult.Read())
                 {
                    // Add to the list of policies - creates a new row for the collection.
                    additionalInterestPolicyData.Add(new AdditionalInterestPolicyData(
                                                           Int32.Parse(PolicyResult["affiliate_id"].ToString()),
                                                           Int32.Parse(PolicyResult["master_package_iden_key"].ToString())
                                                                                     )
                                                    );
                 }
              }
              catch (Exception ex)
              {
                  bError = true;
              }
              finally
              {
                  PolicyResult.Close();
                  objConn.Close();
              }
  
              return additionalInterestPolicyData;
          }
      }
  </code></pre>