dynamic configuration of asp:SqlDataSource?

Discussion in 'ASP .Net' started by =?Utf-8?B?TWFyaw==?=, Mar 2, 2007.

  1. Hi...

    I'm trying to implement roles in various components (i.e. using different
    sets of configuration depending, in this case, on which environment your in).
    Pretty easy to add an AppSettings["role"] variable in most cases.

    The thing that's causing me difficulty are the asp:SqlDataSource components
    in an aspx page. Depending on the role setting, I have a different
    connection string I want to use. Problem is that ConnectionString="<%$ %>"
    only lets you have a fixed lookup for one value in one collenction.

    It doesn't allow ConnectionString="<% %>". <%#
    ConfigurationManager.ConnectionStrings["db"+AppSettings["role"]].ConnectionStrings
    %> is called malformed. I defined my on method on the page to do the lookup
    and while ConnectionString="<%# GetConnectionString() %>" parses fine, it
    doesn't execute.

    So I'm obviously missing something here. How would one choose a
    connectionstring based on some dynamic element?

    Thanks
    Mark
     
    =?Utf-8?B?TWFyaw==?=, Mar 2, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?TWFyaw==?=

    justin Guest

    On Mar 2, 6:45 pm, Mark <> wrote:
    > Hi...
    >
    > I'm trying to implement roles in various components (i.e. using different
    > sets of configuration depending, in this case, on which environment your in).
    > Pretty easy to add an AppSettings["role"] variable in most cases.
    >
    > The thing that's causing me difficulty are the asp:SqlDataSource components
    > in an aspx page. Depending on the role setting, I have a different
    > connection string I want to use. Problem is that ConnectionString="<%$ %>"
    > only lets you have a fixed lookup for one value in one collenction.
    >
    > It doesn't allow ConnectionString="<% %>". <%#
    > ConfigurationManager.ConnectionStrings["db"+AppSettings["role"]].ConnectionStrings
    > %> is called malformed. I defined my on method on the page to do the lookup
    > and while ConnectionString="<%# GetConnectionString() %>" parses fine, it
    > doesn't execute.
    >
    > So I'm obviously missing something here. How would one choose a
    > connectionstring based on some dynamic element?
    >
    > Thanks
    > Mark


    Mark:

    Do you expect to have a known set of connection strings and you need
    to dynamically choose from them? or will you need to modify the actual
    connection string based on some dynamically generated data?
     
    justin, Mar 3, 2007
    #2
    1. Advertising

  3. Hello Mark,

    Not sure I understand your question fully but at a quick glance, I would
    think something like the following would work:
    =-=-=-=-=-=-
    ConnectionStringSettings connectionStringSettings;

    if (User.IsInRole("Admin"))
    {
    connectionStringSettings =
    ConfigurationManager.ConnectionStrings["AdminSqlDataBase"];
    }
    else
    {
    connectionStringSettings =
    ConfigurationManager.ConnectionStrings["UserDataBase"];
    }

    SqlDataSource1.ConnectionString(connectionStringSettings.ConnectionString);
    }

    --
    enjoy - brians
    http://www.limbertech.com


    "Mark" wrote:

    > Hi...
    >
    > I'm trying to implement roles in various components (i.e. using different
    > sets of configuration depending, in this case, on which environment your in).
    > Pretty easy to add an AppSettings["role"] variable in most cases.
    >
    > The thing that's causing me difficulty are the asp:SqlDataSource components
    > in an aspx page. Depending on the role setting, I have a different
    > connection string I want to use. Problem is that ConnectionString="<%$ %>"
    > only lets you have a fixed lookup for one value in one collenction.
    >
    > It doesn't allow ConnectionString="<% %>". <%#
    > ConfigurationManager.ConnectionStrings["db"+AppSettings["role"]].ConnectionStrings
    > %> is called malformed. I defined my on method on the page to do the lookup
    > and while ConnectionString="<%# GetConnectionString() %>" parses fine, it
    > doesn't execute.
    >
    > So I'm obviously missing something here. How would one choose a
    > connectionstring based on some dynamic element?
    >
    > Thanks
    > Mark
    >
     
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=, Mar 3, 2007
    #3
  4. Hi Justin...

    "justin" wrote:
    > Mark:
    >
    > Do you expect to have a known set of connection strings and you need
    > to dynamically choose from them? or will you need to modify the actual
    > connection string based on some dynamically generated data?


    The former - a known set of connection strings that I need to choose from
    dynamically. Brian's response seems to indicate most things dynamic should
    be done in the code behind, not in the aspx (the <%$ syntax being a slight
    exception), but it wasn't clear where to put it in the code behind - in the
    Page constructor, event handlers, or where?

    Coming from an old asp background, this would be pretty easy, but asp.net
    puts more fences around things.

    Thanks
    Mark
     
    =?Utf-8?B?TWFyaw==?=, Mar 4, 2007
    #4
  5. Hello Mark,

    As for the <%# %> like inline expression, it is used for databinding, that
    means, in order to make the expression be executed, you need to call
    "DataBind" method on that control or its container parent control. For
    example, given the following SqlDataSource control and expression:

    ==============
    <div>
    ..............
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString='<%#
    ConfigurationManager.ConnectionStrings["testdbConnectionString"] %>'
    ></asp:SqlDataSource>

    ==============

    You can put code in Page_Init to make the binding expression get executed:

    protected void Page_Init(object sender, EventArgs e)
    {
    SqlDataSource2.DataBind();
    }


    In addition, in ASP.NET 2.0, you have a new feature called "Expression
    Builder". You can define your own inline expression syntax(like the <%$
    Resource %>, <%$connectionstring %> ....) and use it in aspx template. Here
    are some web article and reference discussing on this:


    #The CodeExpressionBuilder
    http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionB
    uilder.aspx

    #ExpressionBuilder Class
    http://msdn2.microsoft.com/en-us/library/system.web.compilation.expressionbu
    ilder(VS.80).aspx

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 5, 2007
    #5
  6. Thanks Steven... The custom ExpressionBuilder was a great suggestion - the
    blog entry even gave all the code.

    -Mark


    "Steven Cheng[MSFT]" wrote:

    > Hello Mark,
    >
    > As for the <%# %> like inline expression, it is used for databinding, that
    > means, in order to make the expression be executed, you need to call
    > "DataBind" method on that control or its container parent control. For
    > example, given the following SqlDataSource control and expression:
    >
    > ==============
    > <div>
    > ..............
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString='<%#
    > ConfigurationManager.ConnectionStrings["testdbConnectionString"] %>'
    > ></asp:SqlDataSource>

    > ==============
    >
    > You can put code in Page_Init to make the binding expression get executed:
    >
    > protected void Page_Init(object sender, EventArgs e)
    > {
    > SqlDataSource2.DataBind();
    > }
    >
    >
    > In addition, in ASP.NET 2.0, you have a new feature called "Expression
    > Builder". You can define your own inline expression syntax(like the <%$
    > Resource %>, <%$connectionstring %> ....) and use it in aspx template. Here
    > are some web article and reference discussing on this:
    >
    >
    > #The CodeExpressionBuilder
    > http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionB
    > uilder.aspx
    >
    > #ExpressionBuilder Class
    > http://msdn2.microsoft.com/en-us/library/system.web.compilation.expressionbu
    > ilder(VS.80).aspx
    >
    > Hope this helps.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
     
    =?Utf-8?B?TWFyaw==?=, Mar 6, 2007
    #6
  7. You're welcome ;-)

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead
     
    Steven Cheng[MSFT], Mar 7, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Herriman
    Replies:
    4
    Views:
    770
    Allan Herriman
    Aug 29, 2003
  2. Mark
    Replies:
    0
    Views:
    861
  3. mosscliffe
    Replies:
    3
    Views:
    9,573
    mosscliffe
    Apr 28, 2006
  4. OceanTragic
    Replies:
    0
    Views:
    1,831
    OceanTragic
    Nov 14, 2006
  5. PeterKellner
    Replies:
    1
    Views:
    208
    mou piya
    Jun 2, 2006
Loading...

Share This Page