0

I have code for changing connection string in app.config. But when i change the database i encounter an error because the linq to sql.dbml is not updated to the database i changed. I need to close the program and open again to take effect the changes. What should i do to update my linq to sql.dbml?

var name = "DbName";
   bool isNew = false;
            string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", name));
            XmlNode node;
            isNew = list.Count == 0;
            if (isNew)
            {
                node = doc.CreateNode(XmlNodeType.Element, "add", null);
                XmlAttribute attribute = doc.CreateAttribute("name");
                attribute.Value = name;
                node.Attributes.Append(attribute);

                attribute = doc.CreateAttribute("connectionString");
                attribute.Value = "";
                node.Attributes.Append(attribute);

                attribute = doc.CreateAttribute("providerName");
                attribute.Value = "System.Data.SqlClient";
                node.Attributes.Append(attribute);
            }
            else
            {
                node = list[0];
            }
            string conString = node.Attributes["connectionString"].Value;
            SqlConnectionStringBuilder conStringBuilder = new SqlConnectionStringBuilder(conString);
            conStringBuilder.DataSource = txtConnectServername.Text;
            conStringBuilder.InitialCatalog = "AlTayerDB";
            conStringBuilder.PersistSecurityInfo = true;
            conStringBuilder.UserID = txtConnectUserId.Text;
            conStringBuilder.Password = txtConnectAdapterPassword.Text;
            conStringBuilder.MultipleActiveResultSets = true;
            node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
            if (isNew)
            {
                doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
            }
            doc.Save(path);
Reynan
  • 163
  • 1
  • 1
  • 11

1 Answers1

0

I suggest you to follow this - How do I (update/insert/remove) the config file during runtime? for complete information about this topic:

Modify existing values in the config file during runtime.

Because the Configuration.AppSettings property is read-only, in order to modify the current application settings value, we must use the XmlDocument class to directly update the application configuration file as an XML document.

Here is the original App.config file:

<configuration>
  <appSettings>
    <add key="Setting1" value="1" />
    <add key="Setting2" value="2" />
  </appSettings>
</configuration>

Here is the code sample to modify the application settings value:

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

foreach (XmlElement element in xmlDoc.DocumentElement)
{
    if (element.Name.Equals("appSettings"))
    {
        foreach (XmlNode node in element.ChildNodes)
        {
            if (node.Attributes[0].Value.Equals("Setting1"))
            {
                node.Attributes[1].Value = "New Value";
            }
        }
    }
}

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("appSettings");

If this does not suite to requirement then check the below references:
Change the value in app.config file dynamically
App.Config change value
update app.config file programatically with ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75