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='<%=Application["ConnString"]%>'
which doesn't work.

This works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>"
but it reads from web.config.

I need to have it read from the application object.
Anyone know the correct systax to do that?

Who is Participating?
Gary DavisDir Internet SvcsCommented:
My idea will not work for server controls. What you have to do is to just set the SqlDataSource1.ConnectionString property on your Page_Load code

            SqlDataSource1.ConnectionString = Application['connString"];


            SqlDataSource1.ConnectionString = getConnString();

No need to have the ConnectionString property in your code-infront:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" >
Gary DavisDir Internet SvcsCommented:
Call a public method that returns the string Application["connString"]. This would allow you to verify the string is actually there (non-null) and handle the error case (provide a default or redirect to error page and/or log the error.

Gary Davis
GaryHoffAuthor Commented:
What is the correct symtax?

doesn't work, get run time error message "Keyword not supported: '<%'.".
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

GaryHoffAuthor Commented:
Add a typecast for string and that worked.  Thanks.
SqlDataSource1.ConnectionString = (string)Application["connString"];

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.
Gary DavisDir Internet SvcsCommented:
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
GaryHoffAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.