Search code examples
asp.netsql-serverglobal-variablesdata-access-layerbll

Creating Global Variable For Web Application - ASP.NET


i'm building my web application to connect with db. so far i have managed to deal with it, (although i didn't build BLL & DAL).

i have a table which has column "id". i know there is a way to declare it in the SQL Server to be incremented automatically. ( but i don't want it).

i want to declare a global application variable that will hold the value.

i have 2 questions:

  1. how i declare it?

  2. where i create it and initialize it ? (i have several login pages).

THANKS!

p.s

it would be helpful if someone will tell me how do i build the DAL with my stored procedures? and for what i need yo use BLL which i cant do in the DAL?


Solution

  • You can use the Application object - it is part of the HttpContext and is directly accessible on any page.

    If you don't want to use it, you may want to write a Globals class (or whatever name you like) that holds static members.

    public class Globals
    {
      public static int Counter { get; set;}
    }
    
    // accessed from other classes:
    Globals.Counter++;
    

    Either approach will not work of you have a web farm or several web applications and will not survive restarts.


    Regardless of these options, the right solution (even if you don't want to use it - can you explain why?), is to use the ID field with the IDENTITY clause.