We help IT Professionals succeed at work.

session variable in ASPX file

bamapie
bamapie asked
on
407 Views
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?

Thanks
Comment
Watch Question

Hi,
Have macro for this declare your macros in web.config,
 <system.codedom>
    <compilers>
      <compiler compilerOptions="/d:DEBUG,PRODUCTION" />
    </compilers>
  </system.codedom>
#if PRODUCTION
Session["WhichConnectionString"]="Get Connection string into "
#endif
#if TESTMODE
Session["WhichConnectionString"]="Connection string"
#endif

Hope this will help you
Regards
Deepu
Anurag ThakurTechnical Manager

Commented:
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
#if DEBUG
  -- get debug or test connection string
#elseif
 -- production connection string
#endif

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

Author

Commented:
Deepu:

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.
hi,

Please refer the following link

http://odetocode.com/Blogs/scott/archive/2005/12/02/conditional-compilation-in-asp-net-2-0.aspx

this will make clear understanding of what i mean.

Deepu
Anurag ThakurTechnical Manager

Commented:
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,

Author

Commented:
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:

      <asp:SqlDataSource
       ID="vwPalletManagerEvents"
       runat="server"
       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

Commented:
have a look at the following link comment no. 12
http://www.codeguru.com/forum/showthread.php?t=372918
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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.

OR

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.