Reputation: 952
I have a page in asp.net (web forms) that up until now only had one database for data.
So all the controls that require something from the database use a datasource and the same connection string.
<asp:SqlDataSource id="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnectionString %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT ... ">
</asp:SqlDataSource>
Up until now I only needed one connection string since I had only one database but this has changed since I want some users to connect to database A and others to database B
What would be the correct way to go about it? Preferably without many changes...
Upvotes: 0
Views: 12502
Reputation: 749
On your page you have SqlDataSource1, and also in your web.config you should have:
<connectionStrings>
<add name="MyConectionString" connectionString="Data Source=127.0.0.1;Initial Catalog=DBNAME;User ID=userName;Password=PASS" />
</connectionStrings>
To add the connection string to your SqlDatasource1, in code behind (cs file) use the following code in onliad methot:
SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["MyConectionString"].ConnectionString;
Upvotes: 1
Reputation: 31630
You can change it programatically after you determine which one you want to use, i.e.
SqlDataSource1.ConnectionString = "whateveryouwant";
It doesn't have to be set inline in the control.
Upvotes: 1