ok so i seem to be getting an annoying bug that i hope you can help me with.
I am using winforms and on an application set up form, I provide functionality to change the connection string. When the different parts of the connection string is changed and the user leaves the textbox, it writes the information to the app.config. This all works like a charm.
I have a button to test the connection, so when this is pressed after the connection string has been changed anytime while the form is still open, I hit an error:
The ConnectionString property has not been initialized
to make this a bit more complex, when i close and reopen the application, the same form is now able to make the connection and all works smoothly. Its almost as if the Config is locking itself from being queried after a change is made.
Is there something i need to do in between making changes to the app.config and attempting to open up a connection using the stored connection string.
here is my code to change the app.config:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["myconnstr"].ConnectionString = textboxCS.Text;
config.Save(ConfigurationSaveMode.Modified);
here is my code to test the connection:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["myconnstr"].ConnectionString;
using (SqlConnection connection = new SqlConnection(constr))
{
try
{
connection.Open();
connection.Close();
MessageBox.Show("Successful Connection");
}
catch (Exception ee)
{
ee.ToString();
MessageBox.Show("Failed Connection");
}
finally
{
connection.Close();
}
this :
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["myconnstr"].ConnectionString;
needed to be replaced with:
string constr = config.ConnectionStrings.ConnectionStrings["myconnstr"].ConnectionString;
The problem was that it was trying to open ConfigurationManager twice
Thanks for all the help guys