473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Creating Custom Configuration Sections

Hi,

I need to have a custom connection string in the web.config because I need to ping to Database only during day time say between 8AM and 5 PM, because of this I thought of creating custom configuration.

so I tried creating a class which extends from the class ConfigurationSection, Please find the coding which I have done.
Expand|Select|Wrap|Line Numbers
  1. class ExtendedConnection:ConfigurationSection
  2.     {
  3.         // Start time of Connection
  4.         [ConfigurationProperty("startTime", IsRequired = true)]
  5.         public string StartTime
  6.         {
  7.             get { return (string)this["startTime"]; }
  8.             set { this["startTime"] = value; }
  9.         }
  10.  
  11.         // End time of Connection
  12.         [ConfigurationProperty("endTime", IsRequired = true)]
  13.         public string EndTime
  14.         {
  15.             get { return (string)this["endTime"]; }
  16.             set { this["endTime"] = value; }
  17.         }
  18.  
  19.         // Connction Name
  20.         [ConfigurationProperty("connectionName", IsRequired = true)]
  21.         public string ConnectionName
  22.         {
  23.             get { return (string)this["connectionName"]; }
  24.             set { this["connectionName"] = value; }
  25.         }
  26.  
  27.         // Connection String
  28.         [ConfigurationProperty("connectionString", IsRequired = true)]
  29.         public string ConnectionString
  30.         {
  31.             get { return (string)this["connectionString"]; }
  32.             set { this["connectionString"] = value; }
  33.         }
  34.  
  35.         public ExtendedConnection()
  36.         { }
  37.  
  38.     }
  39.  
After completing this I registed this in the web.config file as shown below.

Expand|Select|Wrap|Line Numbers
  1. <configuration>
  2.   <configSections>
  3.     <section name="TestExtended" type="ExtendedConnection"/>
  4.   </configSections>
  5.   <system.web>
  6.         <compilation debug="true" targetFramework="4.0" />
  7.     <TestExtended startTime="08:00" endTime="16:00" connectionName="constring" connectionString="server=SQLEXPRESS;Initial Catalog=master;Trusted_Connection=Yes;Integrated Security=SSPI"></TestExtended> 
  8.     </system.web>
  9. </configuration>
  10.  
  11.  
Now when I try to use the registered value "TestExtended" inside <system.web> I am getting error as "Unrecognized configuration section system.web/TestExtended."
where am I doing the mistake, please help. I am using VS2010 express edition
Aug 25 '11 #1

✓ answered by Frinavale

I think you're over complicating your requirement.
Can the user change the time frame for when the databased is pinged?

I've never implemented a class that inherits from ConfigurationSection before...but I do store a lot of application specific settings in my web.config file that I use throughout my web applications.

Like this:
(exert from web.config)
Expand|Select|Wrap|Line Numbers
  1. <?xml version"1.0"?>
  2. <configuration>
  3.   <!-- ..... -->
  4.   <appSettings>
  5.     <add key="startTime" value="08:00" />
  6.     <add key="endTime" value="16:00" />
  7.   </appSettings>
  8.   <!-- ..... -->
  9. </configuration>
Now when I want to retrieve these values somewhere in code I do the following:

(VB.NET)
Expand|Select|Wrap|Line Numbers
  1.  Dim startTime As String = System.Configuration.ConfigurationManager.AppSettings("startTime")
  2.  Dim endTime As String = System.Configuration.ConfigurationManager.AppSettings("endTime")
  3.  

(C#)
Expand|Select|Wrap|Line Numbers
  1.  String startTime = System.Configuration.ConfigurationManager.AppSettings("startTime");
  2.  String endTime = System.Configuration.ConfigurationManager.AppSettings("endTime");
  3.  
-Frinny

2 2082
Frinavale
9,735 Expert Mod 8TB
I think you're over complicating your requirement.
Can the user change the time frame for when the databased is pinged?

I've never implemented a class that inherits from ConfigurationSection before...but I do store a lot of application specific settings in my web.config file that I use throughout my web applications.

Like this:
(exert from web.config)
Expand|Select|Wrap|Line Numbers
  1. <?xml version"1.0"?>
  2. <configuration>
  3.   <!-- ..... -->
  4.   <appSettings>
  5.     <add key="startTime" value="08:00" />
  6.     <add key="endTime" value="16:00" />
  7.   </appSettings>
  8.   <!-- ..... -->
  9. </configuration>
Now when I want to retrieve these values somewhere in code I do the following:

(VB.NET)
Expand|Select|Wrap|Line Numbers
  1.  Dim startTime As String = System.Configuration.ConfigurationManager.AppSettings("startTime")
  2.  Dim endTime As String = System.Configuration.ConfigurationManager.AppSettings("endTime")
  3.  

(C#)
Expand|Select|Wrap|Line Numbers
  1.  String startTime = System.Configuration.ConfigurationManager.AppSettings("startTime");
  2.  String endTime = System.Configuration.ConfigurationManager.AppSettings("endTime");
  3.  
-Frinny
Aug 25 '11 #2
Hi,
I agree with your solution, The solution you have provided would work 100%. Thx for your help.
Aug 26 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert | last post by:
I have an application with custom configuration sections in it's app.config file. Here's a shortened excerpt: <monitors> <monitor...
5
by: rdcpro | last post by:
In reading MSDN docs on creating custom Configuration sections, I found this page: ...
1
by: Paloma GarcĂ­a | last post by:
Dear all, I have created personalized configuration sections in my web project following the instructions described in this page...
0
by: uttara | last post by:
I was going through the following article on msdn 2.0 on how to use the custom configuration section. http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx The custom config section it shows is...
0
by: Michael | last post by:
Hi, I would like to know if it is possible to use the ConfigurationSection class to modify user settings. I have created custom configuration classes that derive from ConfigurationSetion and...
1
by: mcstrini | last post by:
I'm trying to have my application use a special config file which is shared across servers, and want to include a custom section (for URLs of report services associated with database instances). ...
4
balabaster
by: balabaster | last post by:
Okay, I decided that I needed to understand the whole custom configuration file bits and so I've spent some time playing around with it. It seems (in the most) relatively straight forward. However,...
1
by: =?Utf-8?B?SmFzb24gUmljaG1laWVy?= | last post by:
This may have been addressed in another post but I was unable to find what I was looking for. I am trying to create a custom configuration section that is element-centric instead of...
4
by: David Williams | last post by:
I am familiar with creating custom configuration settings and accessing them. However, for a current requirement, I need to control the various settings in my custom configuration section by...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.