Configuration Reading using NetOffice C#.NET

Nov 7, 2012 at 12:08 PM
Edited Nov 7, 2012 at 12:09 PM
  1. Hi all,
  2. i am try to read App.config file many way but i can,t read key and value pair,
  3. App.config
  4. <?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <section name="FilterHashKey" type="AutomationAddin1.ConfigHelper, AutomationAddin1"/>  </configSections>  <FilterHashKey>    <FilterKeys>      <add key="722ACP" value="722ACP" />      <add key="722ARI" value="722ARI" />      <add key="722BIA" value="722BIA" />      <add key="722MHI" value="722MHI" />      <add key="722PSP" value="722PSP" />      <add key="722TCP" value="722TCP" />      <add key="722OCS" value="722OCS" />      <add key="722KCD" value="722KCD" />      <add key="722KRT" value="722KCD" />    </FilterKeys>  </FilterHashKey></configuration>
  5. SearchForm,.cs
  6.     try         
  7.    {               
  8.  ConfigHelper section = (ConfigHelper)ConfigurationManager.GetSection("FilterHashKey"); 
  9.                if (section != null)               
  10.  {                    foreach (FilterHashElement element in section.HashKeys) 
  11.                        MessageBox.Show("The value of Appconfig files:" + element.Key + ":" + element.Value, "Good Morning");                        //Console.WriteLine(String.Format("Key {0}, Value {1} ", element.Key, element.Value));                                     //Console.WriteLine("\nPress any key to exit...");    
  12.                 //Console.ReadLine();        
  13.         }            
  14. }           
  15.  catch (Exception ex)  
  16.           {                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);            }
  17. i am ConfigHelper.cs 
  18. using System;using System.Collections.Generic;//using System.Linq;using System.Text;using System.Configuration;
  19. namespace AutomationAddin1
  20. {     
  21. public class ConfigHelper:ConfigurationSection 
  22.    {        
  23.  /// <summary>        /// The value of the property here "Folders"        /// needs to 
  24. match that of the config file section        /// </summary>    
  25.     [ConfigurationProperty("FilterKeys")]       
  26.  public FilterHashKeyCollection HashKeys      
  27.   {          
  28.   get { return ((FilterHashKeyCollection)(base["FilterKeys"])); }   
  29.      }  
  30.   }     
  31. /// <summary>    /// The collection class that will store the list of each element/item that    /// is returned back from the configuration manager.    /// </summary>    [ConfigurationCollection(typeof(FilterHashElement))]   
  32.  public class FilterHashKeyCollection : ConfigurationElementCollection  
  33.   {       
  34.  protected override ConfigurationElement CreateNewElement()   
  35.      {            return new FilterHashElement();        }       
  36.   protected override object GetElementKey(ConfigurationElement element)  
  37.       {            return ((FilterHashElement)(element)).Key;        }      
  38.    public FilterHashElement this[int idx]      
  39.   {            get        
  40.     {              
  41.   return (FilterHashElement)BaseGet(idx);          
  42.   }        }    }
  43.      /// <summary>    /// The class that holds onto each element returned by the configuration manager.    /// </summary> 
  44.    public class FilterHashElement : ConfigurationElement  
  45.   {        [ConfigurationProperty("key", DefaultValue = "",          IsKey = true, IsRequired = true)]      
  46.   public string Key        {            get            {                return ((string)(base["key"]));            }            set            {                base["key"] = value;            }        }         [ConfigurationProperty("value",           DefaultValue = "", IsKey = false, IsRequired = false)]      
  47.   public string Value        {            get       
  48.      {                return ((string)(base["value"]));            }  
  49.           set            {                base["value"] = value;            }     
  50.    }    }  }
  51. Now i get Following Error read the App.config files
  52. ---------------------------Error---------------------------System.NullReferenceException: Object reference not set to an instance of an object.
       at AutomationAddin1.uctrl_SearchPatients1.btnClearSearch_Click(Object sender, EventArgs e) in D:\New_Prgramms_Mydata\NetOffice\AutomationAddin1\uctrl_SearchPatients.cs:line 85---------------------------OK   ---------------------------