-2

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

Tim M.
  • 53,671
  • 14
  • 120
  • 163
Bhanu Mittal
  • 61
  • 3
  • 11
  • 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
  • 9
  • 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
  • 15
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,783
  • 1
  • 29
  • 53
0

You need to add System.Configuration as a reference:

System.Configuration.ConfigurationManager.ConnectionStrings["XYZ"].ConnectionString
Oskar
  • 1,996
  • 1
  • 22
  • 39
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,215
  • 5
  • 36
  • 62