-2

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

1
  • See 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. Commented Apr 20, 2016 at 18:55

5 Answers 5

2

Import System.Configuration into your project.

then

var connectionString = ConfigurationManager.ConnectionStrings["NameOfString"];
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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;

Comments

0

You need to add System.Configuration as a reference:

System.Configuration.ConfigurationManager.ConnectionStrings["XYZ"].ConnectionString

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.