2

I have VB.net console application. I would like to read the (ConnectionString) from a web.config.

Web.config is located at a particular path in my virtual PC, say "C:/mywebConfig"

<add name="MY_DB" connectionString="Data Source=DATASOURCE;Initial Catalog=DB;Persist 

Security Info=False; User ID=***;Password=****;" providerName="System.Data.SqlClient" />

My code:

Dim connString As String = String.Empty

connString = ConfigurationManager.ConnectionStrings("MY_DB").ConnectionString

Whenever I try to access it, i get the error not set to an instance of an object or something like that :)

Help please.

I tried to add the web.config in my Project, but still get the error.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
JADE
  • 475
  • 4
  • 12
  • 24

3 Answers3

2

If you do indeed want to read the app.config or web.config of another (web) app, take a look at ConfigurationManager.OpenMappedExeConfiguration.

Felipe Miosso
  • 7,309
  • 6
  • 44
  • 55
Arnout
  • 2,780
  • 14
  • 12
0

web.config is used for web applications - you will need a app.config file.

Have a look at this link for the differences:

dotNET - app.config and web.config

Also this stack overflow question what is app.config for

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
0

As Matt suggested for a console app you need to include a app.config file instead. For easy reading of that file you can reference the System.Configuration assembly and then use the System.Configuration.ConfigurationManager.ConnectionStrings property.

For example:

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbconnectionString"].ConnectionString;

EDIT

I've just re-read your post and it might be a good idea to check your app has permission to read .config files from the c:\ drive.

Mike
  • 661
  • 5
  • 10