The ConnectionString property has not been initialized

Posts   
 
    
Posts: 19
Joined: 29-Jun-2017
# Posted on: 14-Jul-2017 05:29:10   

This is repeated from the .Net Core thread but I copied here because it has nothing to do with .Net Core. It's a newbie question.

The connection string is always empty. I overrode it for now (I hard-coded it in ReadConnectionStringFromConfig() in DataAccessAdapter in _Project_DBSpecific). I read that I need to copy the app.config but that did not work. I basically inserted the connection string in all the *config files (including web.Debug.config and web.Release.config) with no success. I also read somewhere that a "webapp does not read the config file automatically". Not sure if this is related.

I got some help:

WayneBrantley wrote:

In startup.cs, in ConfigureServices:

CommonDaoBase.ActualConnectionString = Configuration.GetConnectionString("StringName");

This is for SelfServicing, I am sure adapter is similar. You do not have a 'web.config' to load settings from, so that is why it is not automatically loaded anymore with asp.net core.

Thank you Wayne, but even with this help I can't figure it out confused flushed (I'm using adapter and I'm not using asp.net core - as per Frans suggestion in the thread I am using .net 4.5.2). There is no startup.cs (is this normal?) but there is a Global.asax.cs. If I put

DataAccessAdapter.ConnectionStringKeyName = "ConnectionString.SQL Server (SqlClientXYZ)";

... in Global.asax.cs that's the value I see in ConnectionStringKeyName in ReadConnectionStringFromConfig() but it seems not to find the proper file to read it from. Where is that file? What's its name? Or where would I insert the quoted line? I would guess it's different for Adapter? But there is no ActualConnectionString property in DataAccessAdapterBase.

I think I need more detailed information (apologies for trivial questions). Thanks.

Jezz! I figured it out! If this can help someone: The Connection strings are in the Web.config file! I put

DataAccessAdapter.ConnectionStringKeyName = "ConnectionString.SQL Server (SqlClientXYZ)";

... in Global.asax.cs and it read it from Web.Config (not web.Debug.config nor web.Release.config - I _though _web.config was generated from these 2 files).

Related: In the future, how can I find the problem to this kind of issue myself? For example if I F11 on

private static string ReadConnectionStringFromConfig()
{
    return ConfigFileHelper.ReadConnectionStringFromConfig(ConnectionStringKeyName);
}

... I cannot see the code for ConfigFileHelper.ReadConnectionStringFromConfig. If I could see the code I could perhaps figure it out.** Is this the way to go ?** If so how do I get that source code?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 14-Jul-2017 11:07:56   

Glad you found it simple_smile Web.config includes the debug/release files if I'm not mistaken, but I'm not a webdev so it might be different. For .NET full apps, connection strings can be places in the app/web.config files. You can also pass the connection string in code btw, the DataAccessAdapter class has a ctor which accepts a connection string. You can e.g. create a small factory method which creates the DataAccessAdapter instance and passes a connection string from code to the ctor.

The source is available customers, and can be downloaded from the website: Under 'my account' -> downloads -> v5.2 -> Extras section. You'll find the sourcecode archive there for the runtime libraries, drivers, code generator, type converters, plugins and commandline tools.

Frans Bouma | Lead developer LLBLGen Pro