Link to home
Start Free TrialLog in
Avatar of bamapie
bamapie

asked on

session variable in ASPX file

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
Avatar of deepu chandran
deepu chandran
Flag of Germany image

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
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
Avatar of bamapie
bamapie

ASKER

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
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,
Avatar of bamapie

ASKER

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.
have a look at the following link comment no. 12
http://www.codeguru.com/forum/showthread.php?t=372918
ASKER CERTIFIED SOLUTION
Avatar of bamapie
bamapie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial