tags:

views:

106

answers:

1

How to rewrite Webconfig connection string at runtime.I have input textbox for Server,UserName and Password.Is it Possible to read from these textbox?

+1  A: 

It depends how you are creating your connection but sure it is possible.

Have a look at this site for the connection string particular to the db you are connecting to..

ConnectionStrings.com

For a SQL connection string you'd do something like

 Imports System.Data.SqlClient
 ...
 ...
 Dim conn As SqlConnection = New SqlConnection("Data Source=myServerAddress;Initial  Catalog=myDataBase;User Id=myUsername;Password=myPassword;")
 conn.Open

Replace the bits you want to change with the values from your text box

e.g

 Dim conn As SqlConnection = New SqlConnection("Data Source=" & txtServerAddr.Text & ";Initial  Catalog=" & txtDBName.Text & ";User Id=" & txtUser.Text & ";Password=" & txtPassword.Text & ";")

EDIT following edit to your question

Ahh makes more sense now.

OK, you have two alternatives now. If you have a limited number of connection strings that you are connecting to (maybe Live, Test, Live#2) it would make more sense to add additional connection strings into your web.config and then offer them as a drop down as you enter your web app.

You can read more on how to pull named connection strings out of web.config here.. Microsoft How to: Read Connection Strings from the Web.config File

If you have an unlimited number of possible connection strings then I would avoid web.config completely and build your connection string on the fly as in my original answer

ADDITION

"It's not a good idea to edit web.config at runtime. Realize that any change you make to web.config will result in the application being restarted on your webserver."

CResults