views:

349

answers:

1

Hi

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.

+2  A: 

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:

SqlDataSource1.ConnectionString = DBConnections.DefaultConnection;

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:

public class MySqlDataSource : SqlDataSource
{
  public string _customConnectionStr; //Allow a custom connection still
  public override string ConnectionString {
    //Default the connection if a custom isn't set
    get { return _customConnectionStr  ?? DBConnections.DefaultConnection; }
    set { _customConnectionStr = value; } 
  }
}
Nick Craver
argh
@argh - You can add it in your web.config once for all pages, say `MySqlDataSource` is in the `Web.Controls` namespace and your website compiles to `Web.dll`, then under `<system.web><pages><controls>` in web.config add this: `<add tagPrefix="Custom" namespace="Web.Controls" assembly="Web" />`, in the page just use `<Custom:MySqlDataSource>` instead of `<asp:SqlDataSource>`, change any names you like in all of that.
Nick Craver
I did as you've said and got:The base class includes the field 'SqlDataSource1', but its type (System.Web.UI.UserControl) is not compatible with the type of control (Outsider.Code.SqlDataSource2).
argh
<compilation batch="false"> seems to fix the problem
argh