Be seen. Boost your question’s priority for more expert views and faster solutions
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<appSettings>
<add key="OrderNumber"/>
</appSettings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri=""/>
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400"/>
</providers>
</roleManager>
</system.web>
</configuration>
public class PurchaseOrder850
{
static public void Main(string[] args)
{
string OrderNo = 123;
MethodHelper.ModifyAppConfig(OrderNo);
}
}
public static void ModifyAppConfig(string value)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["OrderNumber"].Value = value;
Thread.Sleep(TimeSpan.FromSeconds(3)); // added this so there's time for the file to be updated, not sure if necessary.
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message + " key value passed for App Config: " + value);
}
}
config.AppSettings.Settings.Add("OrderNumber", value);
config.AppSettings.Settings["OrderNumber"].Value = value;
withif(config.AppSettings.Settings["OrderNumber"]!=null)
config.AppSettings.Settings["OrderNumber"].Value = value;
else
config.AppSettings.Settings.Add("OrderNumber", value);
try {
if(config.AppSettings.Settings["OrderNumber"]!=null)
config.AppSettings.Settings["OrderNumber"].Value = value;
else
config.AppSettings.Settings.Add("OrderNumber", value);
} catch (Exception ex) {
string path=Path.Combine(Application.StartUp, "myLog.txt");
if (! File.Exists(path))
File.Create(path);
using (StreamWriter w = File.AppendText(path))
{
w.WriteLine(ex.Message + ":" + ex.StackTrace);
}
}
ConfigurationManager.RefreshSection("appSettings");
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection("appSettings");
try
{
if (config.AppSettings.Settings["OrderNumber"] != null)
config.AppSettings.Settings["OrderNumber"].Value = value;
else
config.AppSettings.Settings.Add("OrderNumber", value);
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception e)
{
Console.WriteLine(e.Message + " key value passed for App Config: " + value);
}
public static void ModifyAppConfig(string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection("appSettings");
try
{
if (config.AppSettings.Settings["OrderNumber"] != null)
config.AppSettings.Settings["OrderNumber"].Value = value;
else
config.AppSettings.Settings.Add("OrderNumber", value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception e)
{
string filePath = @"C:\Error.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message :" + e.Message + "<br/>" + Environment.NewLine + "StackTrace :" + e.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString() + Environment.NewLine);
}
}
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
I think the issue is that there is no entry for OrderNumber and it needs to be created first