Forums: [SOLVED] get connection string from Web.Config or other source - Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

[SOLVED] get connection string from Web.Config or other source Rate Topic: -----

#1 User is offline   heilman 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 32
  • Joined: 28-February 15
  • Gender:Male

Posted 09 May 2019 - 09:33 AM

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... )
0

#2 User is offline   ChrisN 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 50
  • Joined: 01-November 14

Posted 09 May 2019 - 09:50 AM

Hey Mark,

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

Cheers,

Chris
0

#3 User is offline   Pete K 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 269
  • Joined: 05-February 16
  • Gender:Male

Posted 09 May 2019 - 11:56 AM

View Postheilman, on 09 May 2019 - 05:33 AM, said:

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






----------------------------------------------------------
Pete K.
Lead Web Developer
Fayette County Public Schools | Lexington, Kentucky, USA
0

#4 User is offline   admin 

  • Administrator
  • PipPipPip
  • Group: Admin
  • Posts: 16456
  • Joined: 03-February 03
  • Gender:Male

Posted 09 May 2019 - 12:40 PM

Also this:
https://asprunner.co...nection-string/
Best regards,
Sergey Kornilov
0

#5 User is offline   heilman 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 32
  • Joined: 28-February 15
  • Gender:Male

Posted 09 May 2019 - 03:07 PM

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 );
}
}
0

#6 User is offline   Pete K 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 269
  • Joined: 05-February 16
  • Gender:Male

Posted 10 May 2019 - 02:44 PM

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.


----------------------------------------------------------
Pete K.
Lead Web Developer
Fayette County Public Schools | Lexington, Kentucky, USA
0

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users