Click here to Skip to main content
15,887,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a piece of code (.NET Framework 4.5.2, Enterprise Library 5.0.505.0) where I need to connect to a SQL Server database. However, the DB name may keep changing depending on user's requirement. So, I have the following piece of code to write the connection string dynamically to the app.config file.

C#
public void CreateNewConnectionStringInConfig(string initialCatalog)
{
      SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
      builder.DataSource = ConfigurationManager.AppSettings["Data Source"];
      builder.PersistSecurityInfo = true;
      builder.InitialCatalog = initialCatalog; //This is the DB name
      builder.UserID = ConfigurationManager.AppSettings["User ID"];
      builder.Password = ConfigurationManager.AppSettings["Password"];

      // Get the application configuration file.
      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

      // Create a connection string element.
      ConnectionStringSettings csSettings = new ConnectionStringSettings("UserSelectedConnectionString", builder.ConnectionString, "System.Data.SqlClient");

      // Get the connection strings section.
      ConnectionStringsSection csSection = config.ConnectionStrings;

      // Add the new element.
      csSection.ConnectionStrings.Add(csSettings);

      // Save the configuration file.
      config.Save(ConfigurationSaveMode.Modified);

     // Refresh the section so the new configuration can be re-read
      ConfigurationManager.RefreshSection("connectionStrings");

}


I checked and the connection string is getting created fine in the vshost.exe.Config file while debugging. However, when I am trying to create the Database object, I am getting an error. The code used to create the DB object is as shown below.

C#
public class MyDac
{
    private readonly Database _db;

    public MyDac()
    {
        DatabaseProviderFactory factory = new DatabaseProviderFactory();
        _db = factory.Create("UserSelectedConnectionString");
    }
}


I am getting the following error while trying to create the _db object.

Activation error occured while trying to get instance of type Database, key "UserSelectedConnectionString"<br />
<br />
Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Database, key "UserSelectedConnectionString" --->     Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "UserSelectedConnectionString".<br />
Exception occurred while: while resolving.<br />
Exception is: InvalidOperationException - The type Database does not have an accessible constructor.


Any help will be much appreciated. Thanks!

What I have tried:

1) Upgrading the Enterprise Library version to 6.0.0.0 resolves the issue but that is not an option for me. I have to keep it to version 5.0.505.0.

2) When I hard code the connection string in the App.config file from before hand (rather than writing it during run time), the app works fine. However, I can't do that in real life because the database name will keep on changing.
Posted
Updated 16-Jun-16 10:24am
v2
Comments
CHill60 7-Jun-16 14:44pm    
Try going to Project Properties, Debug tab, and untick the "Enable the Visual Studio Hosting process" checkbox. This will force the program to use the .exe.Config rather than the vshost.exe.Config during debugging (i.e. behave as it would in production during testing). It might overcome this problem
Anish_Chakraborty 7-Jun-16 14:52pm    
Did not work. The connection string was written correctly to the exe.config file rather than vshost.exe.config. But, still got the same error when trying to create the database object.

1 solution

I changed my code as shown below and that started working. However, I do not have any explanation for it:

C#
public class MyDac
{
    private readonly Database _db;
 
    public MyDac()
    {
        _db = new SqlDatabase("UserSelectedConnectionString");
    }
}
 
Share this answer
 
v2
Comments
leodimonio 20-Jul-22 8:03am    
thank you!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900