1

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?

RonenIL
  • 273
  • 3
  • 8
  • 17

4 Answers4

5

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

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

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
1

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

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.

Code Silverback
  • 3,204
  • 5
  • 32
  • 39
  • GUIDs also index very poorly in databases. There are hacks available to make pseudo-GUIDs that can index a little easier, but they're a bit of a pain. A GUID as a PK is a fairly prevalent performance hit on a database. – David Dec 28 '10 at 14:53