We help IT Professionals succeed at work.

session variable in ASPX file

bamapie asked
Last Modified: 2013-12-17
Consider the code below.

See that connection string referenced there with the angle bracket-percent markup?  That's great.  

But my app is going to use ConnectionString #1 if we're running in Production mode, and ConnectionString #3 if we're running in Test mode.  Connection strings are read-only, so it's not like I can use a 3rd connection string "placeholder" that's assigned #1 or #2 at runtime, based on whether we're running in Production mode or Test mode.

So I'd like to put a session variable in there, instead of a read-only connection string.  I want to do something like:

ConnectionString="<%$ Session["WhichConnectionString"].ToString() %>" SelectCommand=...

What should that look like?  How do I go about this?

Watch Question

Have macro for this declare your macros in web.config,
      <compiler compilerOptions="/d:DEBUG,PRODUCTION" />
Session["WhichConnectionString"]="Get Connection string into "
Session["WhichConnectionString"]="Connection string"

Hope this will help you
Anurag ThakurTechnical Manager

in the web.config file there is a setting <compilation debug="true" />
if this value is true means its debug code and if its false its release version of website

so in code you can use the following
  -- get debug or test connection string
 -- production connection string

the above code will get you going as i am also using the same method in my solutions



1.  I don't see where you set PRODUCTION or TEST "on".  I see where they're declared as options, but not where you define which mode you're in.

2.  That's great that you assign the Session variable in the web.config, but my stated problem is that I can't figure out how to use that value inside the ASPX page.

Please refer the following link


this will make clear understanding of what i mean.

Anurag ThakurTechnical Manager

try what i have suggested
there is only one setting in the web config and nothing else except for setting the connection string section as shown in my comment
Hi ,

You can do the configuration in Project-> properties also,
Please refer the picture attached,


Thank you guys.  Back to my original question:

>ConnectionString="<%$ Session["WhichConnectionString"].ToString() %>" SelectCommand=...

I already have an indicator, in the Session, of whether I'm in TEST or PROD mode.

I like my method, because it doesn't require different values in web.config.  It doesn't require precomp directives or anything like that.

But I want to use my connection string in my ASPX page, in place of where it has web.config connection strings:

       ConnectionString="<%$ ConnectionStrings:RSSQL1ConnectionString %>"

See that connection string between the percent signs?  I don't want that.  I want to use my Session variable connection string there in a clean, elegant way.

Thanks again.
Anurag ThakurTechnical Manager

have a look at the following link comment no. 12
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.