4
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["MyConnectionString",String.Format("DataSource={0};")].ConnectionString=textBox1.Text;
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");

I'm having trouble at line two. I cant seem to get the syntax correct. As you can see, i only want to update the DataSource value only. For example, if current value is Data Source=PC001\SQL2008EXPRESS, i want it to be updated to what the client enters in textBox1.

EDIT: Example ConnectionString

<add name="ERPDatabaseTables" connectionString="metadata=res://*/ERPTables.csdl|res://*/ERPTables.ssdl|res://*/ERPTables.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=PC001\SQL2008EXPRESS;Initial Catalog=MyDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

so want to update Data Source=PC001\SQL2008EXPRESS portion only

StackTrace
  • 9,190
  • 36
  • 114
  • 202

1 Answers1

12

What you actually want is:

Configuration config = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Because it's an EF connection string it's not a normal connection string
// so we pull it into the EntityConnectionStringBuilder instead
EntityConnectionStringBuilder efb = 
    new EntityConnectionStringBuilder(
        config.ConnectionStrings.ConnectionStrings["ERPDatabaseTables"]
            .ConnectionString);

// Then we extract the actual underlying provider connection string
SqlConnectionStringBuilder sqb = 
    new SqlConnectionStringBuilder(efb.ProviderConnectionString);

// Now we can set the datasource
sqb.DataSource = textBox1.Text;

// Pop it back into the EntityConnectionStringBuilder 
efb.ProviderConnectionString = sqb.ConnectionString;

// And update...
config.ConnectionStrings.ConnectionStrings["ERPDatabaseTables"]
    .ConnectionString = efb.ConnectionString;

config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");

This presumes:

  1. Your EF connection string exists in the app's config file

  2. You have a reference to System.Data.Entity

Kev
  • 118,037
  • 53
  • 300
  • 385
  • yes, this is what i want but the changes are not being saved and no exception is thrown. Even after refreshing, the application is still retrieving data using the old dataSource. This is a windows forms application that am installing on client PCs – StackTrace Aug 24 '11 at 12:05
  • @sql.net - sorry missed the line of code to save the config, updated – Kev Aug 24 '11 at 12:09
  • looks like i'm getting there, but getting exception message "Keyword not supported: 'datasource'" – StackTrace Aug 24 '11 at 12:30
  • I should also mention that its an Entity Framework connection string – StackTrace Aug 24 '11 at 12:33
  • @ Kev Ω - Keyword not supported: 'datasource' error still comes up – StackTrace Aug 24 '11 at 14:03
  • @sql.net - what line does the exception get thrown on? Did you replace all of your code with mine? Also your user should not enter `Data Source=PC001\SQL2008EXPRESS` only the value is required, for example: `PC001\SQL2008EXPRESS`. – Kev Aug 24 '11 at 14:49
  • But does it write permanently to the app.config file? I tried it and it wasn't permanent, i don't want this to happen every time i start my application.. – Blank EDjok Feb 20 '14 at 10:32
  • @BlankEDjok - I don't have time to check this right now, but I seem to remember it did permanently save these settings. – Kev Feb 20 '14 at 11:28
  • Even during debugging mode? – Blank EDjok Feb 20 '14 at 11:31