• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1225
  • Last Modified:

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?

0
GaryHoff
Asked:
GaryHoff
  • 3
  • 3
1 Solution
 
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
0
 
GaryHoffAuthor Commented:
What is the correct symtax?

ConnectionString="<%=getConnString()%>"
doesn't work, get run time error message "Keyword not supported: '<%'.".
0
 
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"];

or

            SqlDataSource1.ConnectionString = getConnString();

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


<asp:SqlDataSource ID="SqlDataSource1" runat="server" >
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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.
0
 
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
0
 
GaryHoffAuthor Commented:
Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now