Question

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?

Was it helpful?

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.

OTHER TIPS

Storing the variable is the easy part. Managing your own ID generation and the contention and concurrency issues is the hard part. Good luck.

There really is no such thing as a global variable in ASP.NET. Remember, HTTP is stateless.

The closest you can come is storing something in the Application object:

Application["myvar" ] = x;
x = Application["myvar"];

But even here, this variable is lost when the app needs to restart, which it can do from time to time.

A much better solution for what you describe is a database value.

Incrementing an integer and then throwing that incremented ID into the db is fraught with danger. Multithreading? What happens when the application bounces? Do dev and prod deployments share the same set of numbers?

It sounds like you need a globally unique identifier and can be created outside of the database. That sounds like a job for a GUID. Sure, it takes up more space in the db, but it probably isn't the worst thing you are going to do to the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top