dynamic configuration of asp:SqlDataSource?

G

Guest

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
 
J

justin

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?
 
G

Guest

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);
}
 
G

Guest

Hi Justin...

justin said:
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
 
S

Steven Cheng[MSFT]

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.
 
G

Guest

Thanks Steven... The custom ExpressionBuilder was a great suggestion - the
blog entry even gave all the code.

-Mark


Steven Cheng said:
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.
 
S

Steven Cheng[MSFT]

You're welcome ;-)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top