Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with custom configuration class

Status
Not open for further replies.

andegre

MIS
Oct 20, 2005
275
US
I created a customer configuration class Reports. I then created another class called "ReportsCollection". When I try and do the "ConfigurationManager.GetSection()", it doesn't populate my collection variable. Can anyone see any mistakes in my code?

Here is the collection class:
Code:
    public class ReportsCollection : ConfigurationElementCollection
    {
        public ReportsCollection()
        {
        }

        protected override ConfigurationElement CreateNewElement()
        {
            throw new NotImplementedException();
        }

        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return base.CreateNewElement(elementName);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            throw new NotImplementedException();
        }

        public Report this[int index]
        {
            get { return (Report)BaseGet(index); }
        }
    }

Here is the reports class:
Code:
    public class Report : ConfigurationSection
    {
        [ConfigurationProperty("reportName", IsRequired = true)]
        public string ReportName
        {
            get { return (string)this["reportName"]; }
            //set { this["reportName"] = value; }
        }

        [ConfigurationProperty("storedProcedures", IsRequired = true)]
        public StoredProceduresCollection StoredProcedures
        {
            get { return (StoredProceduresCollection)this["storedProcedures"]; }
        }

        [ConfigurationProperty("parameters", IsRequired = false)]
        public ParametersCollection Parameters
        {
            get { return (ParametersCollection)this["parameters"]; }
        }

        [ConfigurationProperty("saveLocation", IsRequired = true)]
        public string SaveLocation
        {
            get { return (string)this["saveLocation"]; }
        }

        [ConfigurationProperty("recipients", IsRequired = true)]
        public RecipientsCollection Recipients
        {
            get { return (RecipientsCollection)this["recipients"]; }
        }
    }

    public class StoredProcedure : ConfigurationElement
    {
        [ConfigurationProperty("storedProcedureName", IsRequired = true)]
        public string StoredProcedureName
        {
            get { return (string)this["storedProcedureName"]; }
        }
    }

    public class StoredProceduresCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            throw new NotImplementedException();
        }

        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return base.CreateNewElement(elementName);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            throw new NotImplementedException();
        }

        public StoredProcedure this[int index]
        {
            get { return (StoredProcedure)base.BaseGet(index); }
        }
    }
}

And here is the very straight-forward code to create the variable:
Code:
ReportsCollection reportsCollection = (ReportsCollection)System.Configuration.ConfigurationManager.GetSection("ReportGroup");

FYI - all of the "protected override" methods that are in their I added just because they are required when implementing that inherited class.
 
By the way, here is the app.config file...

Code:
  <configSections>
    <sectionGroup name="ReportGroup">
      <section name="Reports" type="ReportsGenerator.ReportsCollection"/>
    </sectionGroup>
  </configSections>
  <ReportGroup>
    <Reports name="DailySanctions" SaveLocation="">
      <StoredProcedures>
        <add StoredProcedureName="RPTDailySanctions" />
        <add StoredProcedureName="RPTNoSanctionsCreated" />
      </StoredProcedures>
      <Parameters>
        <add ParameterName="@FromDate" />
        <add ParameterName="@ThruDate" />
      </Parameters>
      <Recipients>
        <add RecipientName="me@myemail.com"
      </Recipients>
    </Reports>
  </ReportGroup>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top