Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are not wanting to delve into code behind, there is another way you can do this.</p> <p>First read this article on <a href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx" rel="nofollow noreferrer">expression builders</a>. One of my favorite things to bring into to my web-apps! </p> <p><strong>Now for some code:</strong></p> <p>First make a class in your project that contains the following:</p> <pre><code>using System; using System.CodeDom; using System.Web.UI; using System.Web.Compilation; namespace MyNamespace.Web.Compilation { [ExpressionPrefix("code")] public class CodeExpressionBuilder : ExpressionBuilder { public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { return new CodeSnippetExpression(entry.Expression); } } } </code></pre> <p>Then, in web.config register the Expression Builder as follows</p> <pre><code>... &lt;compilation debug="false"&gt; &lt;expressionBuilders&gt; &lt;add expressionPrefix="Code" type="MyNamespace.Web.Compilation.CodeExpressionBuilder"/&gt; &lt;/expressionBuilders&gt; &lt;/compilation&gt; ... </code></pre> <p>(all code above from taken from <a href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx" rel="nofollow noreferrer">here</a> and modified ever so slightly)</p> <p>Finally change your SqlDataSource to the following (C#):</p> <pre><code>&lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='&lt;%$ code: (string)Session["MyConnectionString"] ?? ConfigurationManageer.ConnectionStrings["myDefaultConn"].ConnectionString %&gt;' SelectCommand="SELECT * FROM [Customers]"&gt; &lt;/asp:SqlDataSource&gt; </code></pre> <p>If you wanted (and I would recommend) creating a static class that handles figuring out the connection string for you say something like:</p> <pre><code>public static ConnectionManager { public static string GetConnectionString() { return HttpContext.Current.Session["MyConnectionString"] as string ?? ConfigurationManager.ConnectionStrings["DefaultConnectionStr"].ConnectionString; } } </code></pre> <p>Then your SqlDataSource would be</p> <pre><code>&lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='&lt;%$ code: ConnectionManager.GetConnectionString() %&gt;' SelectCommand="SELECT * FROM [Customers]"&gt; &lt;/asp:SqlDataSource&gt; </code></pre> <p>That way if you ever need to change how get a connection string you can do it one place!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload