0

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...

5
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by CrappyCoder
0

>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.

0

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

Here is your solution
Regards

0

<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.

0

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

Edited by CrappyCoder: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.