views:

32

answers:

3

Hi, Is it possible to insert a connection string into a web.config file if it isnt there? Would this work? The connection string is for a APS.NET user login and create account database.

A: 

Yes it is very possible:

<connectionStrings>
<add name="yourNameHere" connectionString="connectiongStringHere" providerName="nameHere"/>
</connectionStrings>

inside ConfigSections

Eton B.
Thanks, but in the 'add name="yourNameHere" , what would the name be?
pmoth81
It's the name you would give to the connection string, to later make reference to it.You could use ConfigurationManager.ConnectionStrings["yourNameHere"].ConnectionString to retrieve it.
Eton B.
A: 

Put a default connectionstring in your web.config and replace the username/password and database name at runtime as desired.

Jeroen
+1  A: 

How do you mean "insert?" If you mean can you manually edit web.config: yes, you can. You'd add an entry for connectionStrings (if there wasn't one) an then a child node for your specific connectionString.

If you mean can you do it in code: theoretically yes. However, it is normally bad practice, and a pain the rear.

AllenG
If you needed to be able to switch connectionstring at runtime you could chuck it into a database instead of web.config
AndreasKnudsen
@AndreasKnudsen: absolutely.
AllenG