473,385 Members | 1,606 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,385 software developers and data experts.

Dynamic Settings

I am trying to create dynamic settings in a .NET 2.0 C# application. I need
to be able to store settings on the user, but I do not know how many settings
are necessary at design time because the settings are determined by a
business object that is loaded into the app of which there could be one or
many. I would prefer to use the same LocalFileSettingsProvider that the
settings default to so that I do not have to manage it myself.

Here is the code that I currently have:

internal static void SetStringSetting(string name, bool isReadOnly, string
value)
{
SettingsProperty property = new SettingsProperty(name);
property.SerializeAs = SettingsSerializeAs.String;
property.PropertyType = typeof(string);
property.IsReadOnly = isReadOnly;
property.Provider = Properties.Settings.Default.Providers
["LocalFileSettingsProvider"];

SettingsPropertyValue propertyValue = new SettingsPropertyValue(property);
propertyValue.PropertyValue = value;

Properties.Settings.Default.PropertyValues.Add(pro pertyValue);
}

internal static string GetStringSetting(string name)
{
if (Properties.Settings.Default.PropertyValues[name] == null)
return String.Empty;

if (Properties.Settings.Default.PropertyValues[name].PropertyValue == null)
return String.Empty;

return
Properties.Settings.Default.PropertyValues[name].PropertyValue.ToString();
}

This code will create the settings and persist them across the application
session, however, it does not utilize the LocalFileSettingsProvider to save
them to the file. When the application tries to save at close, I also
recieve this error:

Type: ConfigurationErrorsException
Message: The setting {0} does not have either an
ApplicationScopedSettingAttribute or UserScopedSettingAttribute.

I have tried to do:

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add(scopeAttribute, scopeAttribute);

and

UserScopedSettingAttribute scopeAttribute = new UserScopedSettingAttribute();
property.Attributes.Add("UserScopedSettingAttribut e", scopeAttribute);

but I cannot get it to work. Can anyone help me out on this problem?
Jul 12 '07 #1
2 10031
you can handle the SettingsLoaded event and override the settings in an
upper layer.

void Settings_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
SettingNeededEventArgs settingNeededEventArgs = new
SettingNeededEventArgs();
DAL.Setting.FireSettingNeedEvent(this, settingNeededEventArgs);
if (!string.IsNullOrEmpty(settingNeededEventArgs.Conn ectionString))
{
this["ConnectionString"] = settingNeededEventArgs.ConnectionString;
}
}
namespace DAL
{
public class Setting
{
static public event EventHandler<SettingNeededEventArgsOnSettingNeeded ;

internal static void FireSettingNeedEvent(RipAppData.Properties.Setting s
settings, SettingNeededEventArgs settingNeededEventArgs)
{
if (OnSettingNeeded != null)
OnSettingNeeded(settings, settingNeededEventArgs);
}
}
public class SettingNeededEventArgs : EventArgs
{
private string connectionString;

public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}

}
}
handle DAL.Setting.OnSettingNeeded in your upper layer
Another approach is using the Microsoft Enterprise Library.
--
Sheng Jiang
Microsoft MVP in VC++
"Aaron" <Aa***@discussions.microsoft.comwrote in message
news:E7**********************************@microsof t.com...
I am trying to create dynamic settings in a .NET 2.0 C# application. I
need
to be able to store settings on the user, but I do not know how many
settings
are necessary at design time because the settings are determined by a
business object that is loaded into the app of which there could be one or
many. I would prefer to use the same LocalFileSettingsProvider that the
settings default to so that I do not have to manage it myself.

Here is the code that I currently have:

internal static void SetStringSetting(string name, bool isReadOnly, string
value)
{
SettingsProperty property = new SettingsProperty(name);
property.SerializeAs = SettingsSerializeAs.String;
property.PropertyType = typeof(string);
property.IsReadOnly = isReadOnly;
property.Provider = Properties.Settings.Default.Providers
["LocalFileSettingsProvider"];

SettingsPropertyValue propertyValue = new SettingsPropertyValue(property);
propertyValue.PropertyValue = value;

Properties.Settings.Default.PropertyValues.Add(pro pertyValue);
}

internal static string GetStringSetting(string name)
{
if (Properties.Settings.Default.PropertyValues[name] == null)
return String.Empty;

if (Properties.Settings.Default.PropertyValues[name].PropertyValue ==
null)
return String.Empty;

return
Properties.Settings.Default.PropertyValues[name].PropertyValue.ToString();
}

This code will create the settings and persist them across the application
session, however, it does not utilize the LocalFileSettingsProvider to
save
them to the file. When the application tries to save at close, I also
recieve this error:

Type: ConfigurationErrorsException
Message: The setting {0} does not have either an
ApplicationScopedSettingAttribute or UserScopedSettingAttribute.

I have tried to do:

UserScopedSettingAttribute scopeAttribute = new
UserScopedSettingAttribute();
property.Attributes.Add(scopeAttribute, scopeAttribute);

and

UserScopedSettingAttribute scopeAttribute = new
UserScopedSettingAttribute();
property.Attributes.Add("UserScopedSettingAttribut e", scopeAttribute);

but I cannot get it to work. Can anyone help me out on this problem?

Jul 12 '07 #2
That doesn't make any sense. I have several questions about your code:

1. How should the SettingsLoaded event be implemented? I put the event
attachment in the constructor of my form and the method generated there, but
the call: this["MySetting"] does not work because this refers to the form.

2. I don't see how loading a setting solves my problem. I'm trying to save
a setting to the file using the default LocalFileSettingsProvider so that I
can retrieve it later.

3. You don't have anything that subscribes to the OnSettingNeeded event so
I don't understand what the purpose of having it is.

"Sheng Jiang[MVP]" wrote:
you can handle the SettingsLoaded event and override the settings in an
upper layer.

void Settings_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
SettingNeededEventArgs settingNeededEventArgs = new
SettingNeededEventArgs();
DAL.Setting.FireSettingNeedEvent(this, settingNeededEventArgs);
if (!string.IsNullOrEmpty(settingNeededEventArgs.Conn ectionString))
{
this["ConnectionString"] = settingNeededEventArgs.ConnectionString;
}
}
namespace DAL
{
public class Setting
{
static public event EventHandler<SettingNeededEventArgsOnSettingNeeded ;

internal static void FireSettingNeedEvent(RipAppData.Properties.Setting s
settings, SettingNeededEventArgs settingNeededEventArgs)
{
if (OnSettingNeeded != null)
OnSettingNeeded(settings, settingNeededEventArgs);
}
}
public class SettingNeededEventArgs : EventArgs
{
private string connectionString;

public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}

}
}
handle DAL.Setting.OnSettingNeeded in your upper layer
Another approach is using the Microsoft Enterprise Library.
--
Sheng Jiang
Microsoft MVP in VC++
"Aaron" <Aa***@discussions.microsoft.comwrote in message
news:E7**********************************@microsof t.com...
I am trying to create dynamic settings in a .NET 2.0 C# application. I
need
to be able to store settings on the user, but I do not know how many
settings
are necessary at design time because the settings are determined by a
business object that is loaded into the app of which there could be one or
many. I would prefer to use the same LocalFileSettingsProvider that the
settings default to so that I do not have to manage it myself.

Here is the code that I currently have:

internal static void SetStringSetting(string name, bool isReadOnly, string
value)
{
SettingsProperty property = new SettingsProperty(name);
property.SerializeAs = SettingsSerializeAs.String;
property.PropertyType = typeof(string);
property.IsReadOnly = isReadOnly;
property.Provider = Properties.Settings.Default.Providers
["LocalFileSettingsProvider"];

SettingsPropertyValue propertyValue = new SettingsPropertyValue(property);
propertyValue.PropertyValue = value;

Properties.Settings.Default.PropertyValues.Add(pro pertyValue);
}

internal static string GetStringSetting(string name)
{
if (Properties.Settings.Default.PropertyValues[name] == null)
return String.Empty;

if (Properties.Settings.Default.PropertyValues[name].PropertyValue ==
null)
return String.Empty;

return
Properties.Settings.Default.PropertyValues[name].PropertyValue.ToString();
}

This code will create the settings and persist them across the application
session, however, it does not utilize the LocalFileSettingsProvider to
save
them to the file. When the application tries to save at close, I also
recieve this error:

Type: ConfigurationErrorsException
Message: The setting {0} does not have either an
ApplicationScopedSettingAttribute or UserScopedSettingAttribute.

I have tried to do:

UserScopedSettingAttribute scopeAttribute = new
UserScopedSettingAttribute();
property.Attributes.Add(scopeAttribute, scopeAttribute);

and

UserScopedSettingAttribute scopeAttribute = new
UserScopedSettingAttribute();
property.Attributes.Add("UserScopedSettingAttribut e", scopeAttribute);

but I cannot get it to work. Can anyone help me out on this problem?


Jul 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jennie | last post by:
I need to collect dynamic SQL execution times for a production database. However, the totals are not being updated, despite setting the DFT_MON_TIMESTAMP on. The DFT_MON_TIMESTAMP is listed in...
3
by: Guru | last post by:
Hi, does anyone know how to get the dynamic proxy settings that are generated from script run by Internet Explorer? WebProxy.GetDefaultProxy only works for nondynamic proxy settings. Ralf
0
by: Kenneth Jonsson | last post by:
I have posted this in microsoft.public.dotnet.framework.aspnet.webservices without any response. My problem is with connections from client computers with a dynamic proxy settings in IE to my...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
2
by: Sagaert Johan | last post by:
Hi The Dataset wizard in VS2005 generates a setting entry for the connectionstring. The connectionstring is in the settings file. Setting the property to a dynamic value by writing to the...
2
by: Sin Jeong-hun | last post by:
I've read the "How to: Create Application Settings" article (http:// msdn2.microsoft.com/en-us/library/ms171565.aspx) on the MSDN. It looked fine. But this code is only good when all the settings...
28
by: Peter Michaux | last post by:
Hi, I'm playing with dynamic script insertion to make a request to the server for a JavaScript file to be automatically run when it arrives in the browser. It works but... The page caching...
1
by: =?Utf-8?B?Sm9yZGFuIFou?= | last post by:
We did a same code base test between 1.1 and 3.5. In 1.1, when your .NET application reference a library with web reference (dynamic), you can always easily overwrite the default URL by...
6
by: AGP | last post by:
VB.NET 2005 I've been working extensively with saving and loading my settings via the My.Settings class. its worked out great except that i cant figure out how to set a default setting that is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.