Properties.Settings.Default.Variable does not exists in current Context - c#

I have gone through various question regarding Properties.Settings.Default.<My Settings Name> in stackoverflow and google, but none of them resolved my issue.
I want check whether my application is running for the first time or not, If first time then load particular form, else skip it.
I have a piece of code in My MDIParent Form load
bool isFirstRun = Properties.Settings.Default.IsFirstRun;
MessageBox.Show(Properties.Settings.Default.IsFirstRun.ToString());
if (isFirstRun){
OrganizationDetailsFrm childForm = new OrganizationDetailsFrm();
childForm.MdiParent = this;
childForm.Show();
Properties.Settings.Default.IsFirstRun = false;
Properties.Settings.Default.Save();
}
I checked and found that, every time isFirstRun value is false. When I checked in debug mode, I tried to add Properties.Settings.Default.<My Settings Name> in Add Watch, It show The name 'IsFirstRun' does not exist in the current context.

I have faced the same problem in my project.
Possible Reasons:
You may have multiple 'setting' files in your project.
Name space problem can also be the problem (I was unable to figured it out what is exactly is)
Possible Solutions:
Backup your project first.
Delete your existing settings file.
Make a new settings file in the project.
Drag that new settings file into the Properties Folder (Remember don't do this from Visual Studio, do it from project directory). Now it should work.
Thanks,
Muhammad Fuzail Zubari

Related

Application setting doesn't work well

I have a checkbox(Name:tarahi_algouritm) and a button(Name:button1) on my form(Name:frm_choose).I want to save the latest changes on my checkbox as user clicked on the button.it means user run the program and check the checkbox and then click on button and then close the program.when he/she Rerun it,checkbox should be checked.or someway he disable the checkbox and click on button and after another run,checkbox should be disabled.
for this, in application setting(table part) put a checkbox (Name:s_tarahi_algouritm)and choose USER in scope part..as I said changes are apply on checkbox and s_tarahi_algouritm is used for save the latest changes on checkbox.I wrote these codes:
private void frm_choose_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.s_tarahi_algouritm!=null)
tarahi_algouritm= Properties.Settings.Default.s_tarahi_algouritm;
}
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.s_tarahi_algouritm = tarahi_algouritm;
Properties.Settings.Default.Save();
}
but When I make changes on checkbox and close the debug and Rerun it,changes are not applied.
what should I do?where is wrong?I am partly beginner so explain explicit.
thank you all
The problem is the settings files are written out in two parts: one to the application settings (which you can't save to) and the other to the user settings (which you can save to). You need to save the user settings (it gets written to your c:\users{userid}... directory).
Look at the most up-voted response to Farzin's link. It explains the issue as well.
Here's a more thorough explanation: App.config: User vs Application Scope
Here's an example.
I created a webform app and added a Settings file to it (called TestSettings.settings). I added two values:
When you run this application it creates a file in the application directory named the same as your executable with .config appended that contains (among other things) a element and a element. But this file only contains the initial values. If you change the value under the element and call Save() it will not update this file. It will create a file:
c:\Users{username}\AppData\Local{appname}{random_dir_name}{version}\user.config
My code to demonstrate this was:
Console.WriteLine(TestSettings.Default["UserValue"]);
TestSettings.Default["UserValue"] = "def";
TestSettings.Default.Save();
I tested many things like:
Properties.Settings.Default.Properties.Add(new System.Configuration.SettingsProperty("a"));
Properties.Settings.Default.Properties["a"].DefaultValue = "b";
Properties.Settings.Default.Save();
it has not error but do not save. In this link:
C# Settings.Default.Save() not saving?
Answered you must add Properties.Settings.Default.Reload(); after saving, I did that but not changed. It seems no one knows the answer.(I read many articles).
It looks like a cancer to me! I suggest you to easily save your settings to a xml file.
Below i add an easy xml saving method:
using System.Xml.Linq;
And
XElement settings;
try
{
settings = XElement.Load("settings.xml"); //beside the app .exe file
}
catch (Exception) // it is first time and you have not file yet.
{
settings = new XElement("settings");
settings.Save("settings.xml");
}
If you want to add new element:
settings.Add(new XElement("firstKey", tarahi_algouritm.Checked.ToString()));
settings.Save("settings.xml");
If you want to read or edit element:
XElement settings = XElement.Load("settings.xml");
string firstKey = settings.Element("firstKey").Value; //reading value
settings.Element("firstKey").Value = "New Value"; //Edit
settings.Save("settings.xml"); //Save
Remember that firstKey is only a name and you can use another names instead.

Unable to find connection 'MyConnName' for object 'Web.config' in Table Adapter configure Properties

I want to change the query in table adapter but it's not opening & throwing an error "Configure table Adapter Failed" & in property of connectionString it says "Unable to find connection 'ConnStringName' for object 'Web.config'. The connection string could not be found in application settings, or the data provider associated with the connection string could not be loaded"
I have been looking for a solution for this problem for a long time. Thankfully I found a solution that worked for me.
Close the dataset window with the error.
Right click the dataset in the solution explorer.
Select “Open with…” from the context menu.
Select “Source Code (Text) Editor” from the list and press the “OK”
button.
Near the top of the file, you’ll see a element with several elements within it. Most datasets should only have one element. If you are having trouble like I’ve described above, you’ll have at least two and one will be incorrect. Delete it or otherwise fix it, save the file and reopen normally.
This is the page where I found it out.
See the complete answer here
I faced the same problem which is actually a nightmare and triyed Lorena solution and it was not enough to solve the problem.
The missing part is to modify the Dataset using the XML editor changing the DefaultConnectionIndex to be 0 like this:
<DataSource DefaultConnectionIndex="0"
FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass,
Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-
microsoft-com:xml-msdatasource">
thanks to Don who pointed this
https://social.msdn.microsoft.com/Forums/windows/en-US/08a48cad-619d-4421-b584-f2b5550ba6c5/dataset-designer-quotunable-to-find-connection-myconn-mysettings1-for-object-mysettingsquot?forum=winformsdatacontrols
Seems like there no ConnStringName parameter in connectionString section of Web.config
<connectionStrings>
<add name="ConnStringName" providerName="" connectionString=""/>
</connectionStrings>
Updated:
The problem is in missing reference to System.Data.OracleClient. To solve this just add reference on missing provider.
Well Same problem I faced in Windows Forms, I just opened Settings.settings file, found the "ConnectionString" and repaired it.
(As you click in Value field of ConnectionString a button [...] appears in last of Value field click it and repair/test your connection again, in my case I re-entered password again.) .
Hope This Helps.!
ConnStringName is case-sensitive.

How to handle.NET user setting path changes with assembly info changes?

I've noticed that when changes are made to the assemblyinfo.cs the hash that is used to generate the user settings path is changed.
The screen grab shows the two directories that were created when I changed the copyright date from 2014 to 2015.
I don't imagine I will change other items in the assemblyinfo, but I'm pretty sure I will change the copyright if I do another release next year.
What's the best way to handle this?
Thanks in advance.
The ApplicationSettings class has an Upgrade method which will move the existing setting values to the new settings file.
What I have done previously is to have a boolean flag as a setting (named something like UpgradeRequired) which indicates if an upgrade to the settings is required. This is set to true as default.
When the application starts, check this flag and call the Upgrade() method if required (and then set the UpgradeRequired setting to false).
if (Settings.Default.UpgradeRequired)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
Settings.Default.Save();
}

Visual Studio Command for going to a specific item in source control explorer

Im looking for a way to automatically open Source Control Explorer from inside a plugincode.
So far I managed to open it by executing the command
View.TfsSourceControlExplorer
However, this does not seem to accept any arguments.
My goal here is to do something like this:
destination = "$/dev/framework/someFolder";
_dteObject.ExecuteCommand("View.TfsSourceControlExplorer", destination);
Which will them show me Source Control Explorer in the specified destination.
To anwser CSharpie's comment :
Also there seems to be a bug, if you call navigate to a file of the same directory as the explorer currently is in, everything will disappear.
I had the same problem, got two ways of solving this :
Truncate the filename from the path to only Navigate to folders.
Navigate to root first ("$/"), then navigate to the file you want.
Both works fine in VS2013.
And thanks for the "Application.DoEvent()" fix when the SourceControlExplorer's not opened.
Use the following code to show Source Control Explorer in the specified destination:
public void SelectFolder(string path)
{
dte.ExecuteCommand("View.TfsSourceControlExplorer");
Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExplorerExt explorer =
GetSourceControlExplorer();
if (explorer != null)
explorer.Navigate(path);
}
private Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExplorerExt GetSourceControlExplorer()
{
Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt versionControl =
dte.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as
Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt;
if (versionControl == null)
return null;
return versionControl.Explorer;
}
I believe this is not possible. The source explorer detects the team project and drops you at the root of the team project node. $/myproject/ ..
Happy to be proven wrong on this one...

Viewing Properties.Settings.Default variables

I am trying to use Settings.settings to define/persist some vars. For brevity, I've set up a test file to demonstrate the behavior I'm seeing:
First, I define a setting in Settings.settings:
I then have the following code to test changing variableName:
public Form1()
{
InitializeComponent();
string newString = Properties.Settings.Default.variableName;
Properties.Settings.Default.variableName = "This is a new string";
Properties.Settings.Default.Save();
}
Running the above in the debugger for the first time, I grab the current value (the value I set in the Settings.settings window initially) of variableName from Properties.Settings. As expected, newString is set to "This is a string". Good.....
After executing the next two lines, the debugger shows variableName changed to "This is a new string". Good....
I then run the app through the debugger again. I hit the string newString line and, prior to execution, newString is undefined (of course). Good....
As soon as I execute...
string newString = Properties.Settings.Default.variableName;
... and on subsequent executions of the code, the actual value of variableName is defined as "This is a new string" (Good...as expected).
I then go back to the Settings.settings window. variableName has not changed - it's still "This is a string". I've even closed VSE 2012 and re-opened the project. Settings.settings never changes.
Where is the new value being stored? I've checked all of the .config files ([appname].exe.config, [appname].vshost.exe.config, app.config, and the Settings.settings file) and the new value, "This is a new string" isn't anywhere to be found.
In summary, I'm getting the result I desire from the code, but I can't seem to view the result at design time other than to check the value of the var in the debugger. This seems not only peculiar to me, but impossible.
What am I missing/where am I not looking? I would fully expect the value of variableName to change in the Settings.settings window, but it never does. I've looked everywhere on StackOverflow/Google and can't seem to find the answer.
Thanks in advance....
The original value that you configured via Settings.settings is stored in a .config file alongside your executable's assembly. This will never change unless you modify the Settings file directly via Visual Studio; it's essentially a read-only file.
The user's customized setting is stored in a separate config file within the user's profile. The location of this file depends on your assembly's metadata. For example, on Windows 7/Vista the location might look like:
C:\Users\<user name>\AppData\Local\<company name>\<assembly name>\
AssemblyName\<version>\user.config
If you haven't customized the company name in your assembly's metadata then it may default to Microsoft. Also note that AppData is a hidden folder that may not be visible in Windows Explorer depending on your view settings.
I am not sure if I understand your question. That variable content stay persistent. Thats it. Why you would set a persistent variable to change it later?

Categories