Hi

Please help me out in providing connection string for the SQL datasource at runtime...i need to connect to different database based on the login option....i removed the connection string in the web.config file and tried to select the connection string during page load but it shows an error...can this problem be solved? Please help me out...need to finish my project within 2 days....if there is any alternative to select the database during the runtime please do let me know that too


Thank u folks...

Recommended Answers

All 4 Replies

>Please help me out in providing connection string for the SQL datasource

You have to store the connection strings into a web-config or any text/xml document.

Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HML2010.mdf;Integrated Security=True;User Instance=True"

Here is your solution
Regards

<connectionStrings>
add name="LocalSqlServer" connectionString="Server=your DataBaseServername;Database= Your DataBase Name; Integrity Security=true; providerName="System.Data.SqlClient"/>

OR

<add name="MainConnStr" connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"/>
</connectionStrings>

These connection string's may help you.

Further to the above post you will need to add the following in your webconfig:

<connectionStrings>

add name="LocalSqlServer1" connectionString="Data Source=myServerAddress1;Initial Catalog=myDataBase1;User Id=myUsername1;Password=myPassword1;"/>

<add name="LocalSqlServer2" connectionString="Data Source=myServerAddress2;Initial Catalog=myDataBase2;User Id=myUsername2;Password=myPassword2;"/>

</connectionStrings>

And in your code-behind...

using System.Configuration;
.
.
.

string sqlServer;
if(LoginOption ==1){ 
sqlServer = ConfigurationManager.ConnectionStrings["LocalSqlServer1"].ConnectionString;}

elseif(LoginOption ==2)
{sqlServer = ConfigurationManager.ConnectionStrings["LocalSqlServer2"].ConnectionString;}

You now have different connection strings (which reside in the Webconfig file) being used for different Login Options

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.