-2

How can we retrieve connection string information from web.config file? I am unable to write the code for it.

Tim M.
  • 52,666
  • 12
  • 118
  • 157
Bhanu Mittal
  • 41
  • 3
  • 10
  • See http://stackoverflow.com/a/13043569/453277 and many other questions. Your question is likely to be closed unless you show some research effort and you show how those existing answers do not address your question. – Tim M. Apr 20 '16 at 18:55

5 Answers5

2

Import System.Configuration into your project.

then

var connectionString = ConfigurationManager.ConnectionStrings["NameOfString"];
Jared Lovin
  • 543
  • 11
  • 24
0

You can have a method of a string and call the string in your web.config file

private string Get_ConnectionString
{
  return System.Configuration.ConfigurationManager.ConnectionStrings["NAME OF STRING"].ConnectionString;
}
walangala
  • 231
  • 1
  • 4
  • 14
0

Using ConfigurationManager class you can read settings in web.config and app.config.

This class have a property called ConnectionStrings which gets the ConnectionStringsSection data for the current application's default configuration.

Arturo Menchaca
  • 15,395
  • 1
  • 28
  • 50
0

You need to add System.Configuration as a reference:

System.Configuration.ConfigurationManager.ConnectionStrings["XYZ"].ConnectionString
Oskar
  • 1,879
  • 1
  • 16
  • 34
0

Since you have mentioned Web.config, i assumed its for a web project. You need to add reference to System.Configuration namespace to your web project

static internal string connString = System.Configuration.ConfigurationManager.ConnectionStrings["YourConnection"].ConnectionString;
cableload
  • 4,051
  • 5
  • 33
  • 58