Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Distributed Applications / May 2006

Tip: Looking for answers? Try searching our database.

class library

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CSharpguy - 15 May 2006 15:31 GMT
I'm creating a Class Library that will be used by multiple projects, I want
to read a config file (app.config) for my database connection string(s), how
can i get the class library to read the app.config file for my connection
string?
Michael Nemtsev - 15 May 2006 20:03 GMT
Hello CSharpguy,

Just using System.Xml namespace

C> I'm creating a Class Library that will be used by multiple projects,
C> I want to read a config file (app.config) for my database connection
C> string(s), how can i get the class library to read the app.config
C> file for my connection string?

---
WBR,
Michael  Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
CSharpguy - 15 May 2006 22:19 GMT
I did, and the connection string is coming back blank from my app.config file.
its not reading the file for the database connection

> Hello CSharpguy,
>
[quoted text clipped - 11 lines]
> "At times one remains faithful to a cause only because its opponents do not
> cease to be insipid." (c) Friedrich Nietzsche
Jay B. Harlow [MVP - Outlook] - 16 May 2006 13:05 GMT
CSharpguy,
You read the app.config in a class library the exact same way you would in
an application:

   using System.Configuration;

       // .Net 1.x
       String connectionString =
ConfigurationSettings.AppSettings["MyExcelFileName"];

       // .Net 2.0
       String connectionString =
ConfigurationManager.ConnectionStrings["MyExcelFileName"].ConnectionString;

   <?xml version="1.0" encoding="utf-8" ?>
   <configuration>
     <appSettings>
       <add key="" value=""/>
     </appSettings>
     <connectionStrings>
       <add name="" connectionString="" providerName="" />
     </connectionStrings>
   </configuration>

In the .Net 2.0 case be certain to add a reference to the
System.Configuration assembly.

As you may have noticed above .Net 2.0 includes a new connectionStrings
section in the app.config for storing connection strings. I recommend using
this new section as it leverages the new DbProviderFactory methods.

The trick to reading from the app.config is, that a class library doesn't
have its own app.config, it uses the one from the application (the exe). So
you need to be certain the app.config for the executable contains your
settings!

Signature

Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

| I'm creating a Class Library that will be used by multiple projects, I want
| to read a config file (app.config) for my database connection string(s), how
| can i get the class library to read the app.config file for my connection
| string?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.