GaryHoff
asked on
setting asp:SqlDataSource ConnectionString from Application object
I want to set my asp:SqlDataSource ConnectionString from Application["connstring"] instead of using web config.
I'm trying:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%=Appli cation["Co nnString"] %>'
which doesn't work.
This works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStri ng %>"
but it reads from web.config.
I need to have it read from the application object.
Anyone know the correct systax to do that?
I'm trying:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%=Appli
which doesn't work.
This works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStri
but it reads from web.config.
I need to have it read from the application object.
Anyone know the correct systax to do that?
ASKER
What is the correct symtax?
ConnectionString="<%=getCo nnString() %>"
doesn't work, get run time error message "Keyword not supported: '<%'.".
ConnectionString="<%=getCo
doesn't work, get run time error message "Keyword not supported: '<%'.".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Add a typecast for string and that worked. Thanks.
SqlDataSource1.ConnectionS tring = (string)Application["connS tring"];
Note for others looking at this. Put the above line BEFORE any test for "Page.IsPostBack". It must be reset for every call, expecially if you are using AJAX.
SqlDataSource1.ConnectionS
Note for others looking at this. Put the above line BEFORE any test for "Page.IsPostBack". It must be reset for every call, expecially if you are using AJAX.
Here's a tip. This is what we do on our web. We keep our connection strings in an XML file. When a page needs to get the connection string, it calls a routine we have that loads the connection string from Cache. If it is not in cache, it reads the XML file to get the string and then saves it in Cache.
If we need to switch our databases like to our DR site, we have a web page that lets the DBAs modify the XML file to point the connection string to the other database server. The Cache object has the file as a dependency so saving the file purges it from Cache forcing it to be reloaded and immediatly, the web pages are now on the new server.
Gary Davis
If we need to switch our databases like to our DR site, we have a web page that lets the DBAs modify the XML file to point the connection string to the other database server. The Cache object has the file as a dependency so saving the file purges it from Cache forcing it to be reloaded and immediatly, the web pages are now on the new server.
Gary Davis
ASKER
Thanks.
Gary Davis