Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
2  
Whats your reason for not using db IDENTITY column? – InSane Dec 28 '10 at 14:44
1  
Regarding your PS - I suggest ask a new question for it. – Oded Dec 28 '10 at 14:46

5 Answers

up vote 3 down vote accepted

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.

share|improve this answer
i'm now starting tests on the app, and the identity keeps incementing ? – RonenIL Dec 28 '10 at 15:35
@RonenIL - Isn't that what you wanted? – Oded Dec 28 '10 at 16:48

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

share|improve this answer

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.

share|improve this answer

i know there is a way to declare it in the SQL Server to be incremented automatically. ( but i don't want it)

Why?

share|improve this answer

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.

share|improve this answer
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.