I’ve got 3 connection strings in my app:
<add name="DBConnectionString" connectionString=""/>
<add name="ADConnectionString" connectionString="" />
<add name="TestDBConnectionString" connectionString="" />
now in the code i do:
public static string DefaultConnection
{
get
{
// this is used so that there will be no chance of modifing the original data when testing locally
if (HttpContext.Current.Server.MachineName == "xxx")
return ConfigurationManager.ConnectionStrings["TestDBConnectionString"].ConnectionString;
else
return ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
}
}
and this works in code behind, but when I do ordinary SqlDataSource and then do i like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%# DBConnections.DefaultConnection %>" />
I get: The ConnectionString property has not been initialized.
What am I doing wrong? AFAIK this should get the value I want…
I don’t want to do in connections section as I WANT TO BE SURE that I don’t mess up the production server since I’m in the same network… I could do the whole publishing project but that’s waaaaaaaaaaaay too much work.
Unless you are databinding the container the SqlDataSource is in, the
<%# %>syntax won’t work (because that’s binding syntax, and the property is never bound), so your issue is that the ConnectionString property just isn’t getting set at all.I’d set this in the codebehind, or alternatively, have a completely different config for production. For the code behind:
One more option is, if you use the same connection in tons of places, just make a control that inherits from SqlDataSource and override the ConnectionString property, set it only in that getter/setter and just use that control everywhere instead.
Something like: