This topic is locked
[SOLVED]

 get connection string from Web.Config or other source

5/9/2019 5:33:27 AM
ASPRunner.NET General questions
H
heilmanmd author

In the after init event ASP .NET Ver 9.8 c# looking to pull/get the DB connection string from the Web.Config or some other source.
I tried the following in the event code
// Addding the required Microsoft reference

using System.Configuration;
// then calling the appropriate method to get the connection string

string cs = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
however this isn't working as the Microsoft reference needs to be added as part of the event prior to any code
So Check out the SOLVED solution at the end of this post..
I put together all the pieces of the puzzle into a nicely documented, guaranteed to work code example
Just cut and paste and have at it...
Best

Fellow ASP Runner .NET user 4 + yrs and running... ( no pun intended... )

M
Mack88DevClub member 5/9/2019

Hey Mark,
I too was hunting around for a place to put a reference ... this forum post Third Party DLLs might help.
Cheers,
Chris

Pete K 5/9/2019



In the after init event ASP .NET Ver 9.8 c# looking to pull/get the DB connection string from the Web.Config or some other source.
I tried the following in the event code
// Addding the required Microsoft reference

using System.Configuration;
// then calling the appropriate method to get the connection string

string cs = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
however this isn't working as the Microsoft reference needs to be added as part of the event prior to any code
Any thoughts / feedback on how best to proceed most appreciated.
Mark H. Big Tmber, MT - USA



This should work if you include the fully qualified name, i.e.:

System.Web.Configuration.WebConfigurationManager.ConnectionStrings

H
heilmanmd author 5/9/2019

OK... here's all the gory details in PLAIN computer GEEK talk on how to do this...
Enjoy...

1 First off , need to add a DLL to the CommonEvents.cs file in your ASP .NET Runner Project, i.e.



Using System.Configuration
This is needed in the after app init event.... ( which is part of the CommonEvents.cs )
so... Go to your project Folder i.e. the one that holds the .NETR file and in ./source directory
if there's a CommonEvents.cs file there open it
if not create one
if there's a file after all the other using statements.... add
using System.Configuration
if you created a new file, then add
using System.Configuration
it will be the first and only line in the new file...
Above will now add the DLL to the ASP Runner generated CommonEvents.cs file
so that in the after init event can use
System.Configuration.Configuration rootWebConfig =

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(urlval);

2 if using an IIS default and web "virtual directory" not wise, i.e. best to use C:\inetpub\wwwroot\....



in my case I'm running windows 10 pro in VM and was pointing to shared DropBox dir... so changed back to using the Windows C drive to make sure things worked just to be on the safe side Microsoft and all...


3 here is the code with lots of comments that is working, copy paste and have at it...


// ** IMPORTANT, REALLY REALLY IMPORTANT PIECE OF THE PUZZEL ***

// make sure to include the Using System.Configuration DLL in the project file ./source directory CommonEvents.cs file

// if there isn't one, create one

// then add the following reference
// using System.Configuration ;
// to it...

// ****
// *
HEADS UP !!!!!! ****

// get the web site ROOT url value example

// be aware, no Http:// no DNS ... just the root directory off of IIS is all that is needed...
string websiteroot = "/nyegeo" ;
// ****
System.Configuration.Configuration rootWebConfig =

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(websiteroot);

System.Configuration.ConnectionStringSettings connString;

if ( rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0) {
// connection string name is retrieved from the WEB.CONFIG file... which is in the web sites' IIS directory

//

// <connectionStrings>

// <add name="ODBC_EXCHUAT" connectionString="DSN=TEST;UID=XXXXXXX;PWD=YYYYYYY;Driver=C:\db2driver\BIN\db2clio.dll" />

// </connectionStrings>

//

// so in the above case the connection string name is ODBC_EXCHUAT

//
string conn_name = "ODBC_EXCHUAT";
connString = rootWebConfig.ConnectionStrings.ConnectionStrings[conn_name];

if ( connString != null) {

// sample of what get back: DSN=TEST;UID=XXXXXXX;PWD=YYYYYYYY;Driver=C:\db2driver\BIN\db2clio.dll

// may be different depending on the DB & driver using... but you get the idea....

MVCFunctions.Echo( connString.ConnectionString );

}

}

Pete K 5/10/2019

I guess I don't understand why you think you need all that code. Sergey pointed out the easy solution. Basically, all you need is this line:

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["your_connection_string_name_here"]


I've tested this and it works.