1

Is it possible to insert a connection string into a web.config file at runtime, if it isn't there? Would this work?

The connection string is for a ASP.NET user login and create account database.

2
  • Do you want to manually or programatically add the connection string? Commented Aug 5, 2010 at 16:08
  • The solution I provided is what you're looking for. I added further explanation for your convenience :) Commented Aug 5, 2010 at 16:37

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you needed to be able to switch connectionstring at runtime you could chuck it into a database instead of web.config
0

Yes it is very possible:

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

inside ConfigSections

2 Comments

Thanks, but in the 'add name="yourNameHere" , what would the name be?
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.