0

Is it possible use App.config's value in Connectionstring. Example is mentioned below.

<appsettings><add key="UserName" Value="abc" /></appsettings>

<connectionstring><add name="Conn" connectionString="Server=test;   Database=test; Uid=UserName; Pwd=test123;" />

So as you can see that I have defined the Username in appsettings and I want to see that into the connection string

Any help is appreciated.

DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
user509685
  • 55
  • 1
  • 6

2 Answers2

1

As I know, it's impossible. You can use different App.config files for different configuration (Debug, Release) and set your connection string in them. Look here or here

Community
  • 1
  • 1
Backs
  • 24,430
  • 5
  • 58
  • 85
0

You can use String.Format().
Just change Uid=UserName; to Uid={0};".

String connectionString = String.Format(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString, ConfigurationManager.AppSettings["UserName"].Value);

G_hi3
  • 588
  • 5
  • 22
  • This is probably the "best" option for what you are trying to do but that means you HAVE to manipulate the connection string every time and can't simply use it. I would look into a utility to tokenize your app.config so that those values are always the same. – Jamie Babineau Aug 04 '15 at 11:08