My application Settings get reset after changing product version - c#

I have created an application using Settings from Project Properties. This setting has been used to store some boolean and string variables. I know the settings are user base. So, it can be stored separately for each windows user. I have also added a feature to update application from web server. But, recently i have notice that when i have change the product version then all the settings are get reset. It should not be. How to avoid resetting all setting on version change.
This question is not for only c#. this problem also occurred in vb.

You can try Settings.Upgrade() or ApplicationSettingsBase.GetPreviousVersion
Note: You must call only once this method.
Client Settings

Related

How to read Azure web site app settings values

I am trying to configure some key/value pairs for my Azure web application using app settings section on Windows Azure preview portal.
Now I am trying to read values like below
ConfigurationManager.AppSettings["MyWebApp.DbConnectionString"];
but it returns null values.
Reading app settings from Web.config in my web application works fine.
I found the solution.
Keep values in web.config as well as in Azure App setting. When you are running/debugging application on your local environment it picks values from web.config.
When you deploy application on Azure it picks values from App setting.
//Below code work for both.
ConfigurationManager.AppSettings["KeyName"]
Keep key name same in web.config as well as in Azure app setting.
In Azure, there are a few different ways of retrieving Application Settings and Connection Strings. However, connection strings work a little differently than vanilla application settings.
Application Settings can be retrieved by any method, regardless of whether or not they are present in the Web.config file.
Connection Strings can also be retrieved by any method if the string is defined in Web.config. However, if the connection string is NOT defined in Web.config, then it can only be retrieved using the Environment Variable method.
Retrieving as Environment Variable
Environment.GetEnvironmentVariable("APPSETTING_my-setting-key");
Environment.GetEnvironmentVariable("SQLAZURECONNSTR_my-connection-string-key");
Note that the keys must be prepended with a string designating their type when using this method.
All Application Settings use the APPSETTING_ prefix.
Connection Strings have a different prefix depending on the type of database selected when creating the string in the portal:
"Sql Databases" --> "SQLAZURECONNSTR_my-connection-string-key"
"SQL Server" --> "SQLCONNSTR_my-connection-string-key"
"MySQL" --> "MYSQLCONNSTR_my-connection-string-key"
"Custom" --> "CUSTOMCONNSTR_my-connection-string-key"
For a full overview, see the Windows Azure Web Sites documentation.
System.Environment.GetEnvironmentVariable("SERVICEBUS_CONNECTION")
works great!

DNN Database Repository Reuse in Console Application

I have a project based on the Chris Hammond, Christoc, module template. I have a ton of code that I use to access data an external database. In my repositories I change the database from the default to whichever I need for that particular object. I do so with code that looks like this:
using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY))
{
var rep = ctx.GetRepository<Product>();
products = rep.Get().ToList();
}
The default database is switched in the call to .Instance(). The repositories are used by my custom DNN modules. The repository is part of the solution that contains multiple custom modules. When I compile and install using the Extensions part of DNN, everything works well. In the code above, MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is found in a file MyModuleSettingsBase.cs file of my module solution. It is set to a simple string like "ProductDatabase". In the solution for the base DNN install (not the module solution), within the web.config file, there is a value in <connectionStrings> with name="ProductDatabase" which contains the actual connection string. This all links up fine on the DNN website.
Now I am writing a console application that does some monitoring of the site. I want to access the database to check values in the product table. I would like to reuse all of the repository code I have written. In an attempt to do so, I added a reference to the MyModules.dll file so I would only have one copy of the base code. This works to give me access to all the objects and the associated repositories but when I attempt to query data it fails. When debugging I can see that it fails on the line:
using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY))
When viewed in a debugger, the string value MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is correctly set to "ProductDatabase" but the code is unable to link this with the actual connection string. I don't know where it would be checking for the connections string when running from my console application. I attempted to put a <connectionStrings> section into my App.config file but this didn't do the trick.
Is it possible to have MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY map to the connection string in an external application which references the DLL?
If so, where can I set the value of my connection string so it matches up to the key value stored in MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY?
I was faced similar problem 3 months ago, at that time I want to use DNN core libraries in my console application but I was failed.
I placed my queries in DNN official forum website and I got a valid response from Wes Tatters (DNN MVP).
Here is the post link: Reference URL
As your requirement of monitoring, I suggest you to create DNN Schedule Application. You can schedule it within DNN (Host->AdvancedSettings->Schedule), even good point is that you can use your repositories (DNN Libraries) in that schedule application.
I hope it solved your problem. Let me know if you have any questions.

app.config not loading when App is on StartUp

I have a problem. If I run my application by clicking on it, it loads the saved app.config settings.
However, I need to have the Application run at Startup. Got this working too, but when it loads it does not load the saved settings - just the default ones.
The first time my Application loads with the default settings I require the user to login to there account through a REST API, grab some data and store it. I then set
Properties.Settings.Default.is_installed = true
but when the app runs in Startup it loads the default (false).
Has anyone experienced this? Any help would be appreciated!
If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:
Properties.Settings.Default.is_installed = true;
Properties.Settings.Default.Save();
Here is MSDN Reference 1.
Settings that are application-scoped are read-only, and can only be
changed at design time or by altering the .config file in between
application sessions. Settings that are user-scoped, however, can be
written at run time just as you would change any property value. The
new value persists for the duration of the application session. You
can persist the changes to the settings between application sessions
by calling the Save method.
Here is MSDN Reference 2.
Saving User Settings at Run Time:
Application-scope settings are read only, and can only be changed at
design time or by altering the .exe.config file in
between application sessions. User-scope settings, however, can be
written at run time, just as you would change any property value. The
new value persists for the duration of the application session. You
can persist changes to user settings between application sessions by
calling the Settings.Save method. These settings are saved in the
User.config file.

User settings disappear on outlook upgrades

i'm developing an outlook plug in where users set a list of settings like credentials and site url...
I'am saving this settings in settings.settings in my windows application project.
after installing the add in the config file is set under C:\Users\ user \AppData\Local\Microsoft_Corporation\ project name \ * < outlook Version> * \user.config.
My problem is when performing update for the microsoft office outlook the outlook version changed so my add in will not be able to find the user settings.
How can i pass this problem???
Is it the best practice to save the user settings in settings.settings file?
When I ran into a similar problem a while back, I found this page helpful: http://www.devx.com/dotnet/Article/33944/0/page/4.
It basically requires calling:
<your assembly>.Properties.Settings.Default.Upgrade();
I remember that there is a My.Settings.Upgrade method, which transfers settings between assembly updates. I'd rather use an own settings class and a path about which I decide.
You can also decide to write your own upgrade routine in the case of an major application update.

Storing application settings in C#

What is the best practice to store application settings (such as user name and password, database location ...) in C# ?
Hint: I am new to .net and C#
Application Configuration Settings that are application wide (non-user specific) belong in either app.config (for Desktop apps) or web.config (for Web apps).
Encrypting sections of a web.config file is quite simple as outlined in this Super Simple Example.
If you need to store User specific settings (like application settings, etc.) or Application wide settings not related to application configuration you can use a Settings file as described here:
User Settings in C#
I'm not sure what version of .net/Visual Studio it was introduced in, but you can right click on your project, choose 'Add New Item' and select 'Settings File' from the "Add New Item" window. This provides your project with a (named by default) Settings.settings file that you can configure all the settings you want to expose in.
You can define settings that you create to be either Application or User which means you can use this single interface to control global and user settings. Once you've created a setting in the Settings.settings file using the editor that Visual Studio provides, you can access it in code like this:
// Get a Setting value
var valueOfSetting1 = Settings1.Default.Setting1;
// Modify and save a Setting value
Settings1.Default.Setting1 = "New Value";
Settings1.Default.Save();
First option is the registry. It is easy, but it is not too safe for passwords. Another option is using a file that you create. This too isn't safe, unless you want to implement cryption.
Next option is using the Application Settings. This is also quite simple, but there are a few catches. First, right click on your project and go to Properties. There, under the Settings tab, you can store variables to which you can access from your program by
string password = Properties.Settings.Default.Password
You can also change them the same way, but ONLY IF the scope is set the User. WHen the scope is application-wide, VS does not allow you to change these variables for some odd reason. To save changes, you must call Save() as follows:
Properties.Settings.Default.Save();
These are saved in the User Data folder under C:\Documents and Settings\'Current User'\Local Settings\Application Data\
Another option would be to include them in your database, but since you are also storing your database location, this might not work for you.
I think app.config (non web app) or web.config (web app).
These sorts of settings usually land in Application Configuration Files (web.config, app.config).
http://www.devasp.net/net/articles/display/679.html
If you are storing passwords, you might also need to encrypt the configuration section in question.
http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx
http://msdn.microsoft.com/en-us/library/ff650304.aspx
Note if you use app.config, you will see it get renamed to ..config, depending on if your output produces a DLL or an EXE.
As with the above replies suggest, app.config or the web.config is the best place for app settings.
If you need a more robust way of xml style tags for database, server settings and the like, you can use the configurationSection and create custom sections.
http://www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx
For database passwords, the way i do it is have an encrypted string in the xml tag value and decrypt then when reading them, that way you dont expose the passwords.
appsettings config file, ini file(nini), embeddable database(sqlite,berkelydb/etc..),whatever method you like, it depends on your application size/performance consideration and design.

Categories

Resources