Why not set the database column to be an identity field then the database will increment the value for you and you want have to code around the problem that will occur if two users try to create a record at the same time?
You set the start number for an identity column in the column design so its not really appropriate for your problem. Just make sure you handle the error if two users try to insert the same id at the same time.
MCSD, MCPD, MCTS
Marked as answer by programercek on Sep 28, 2010 01:02 PM
You dont want to be creating a different table for every user that would be a nightmare. You could store the starting number for a user and add that onto the number in the autoincrement field when displayng it, but I think your existing solution of manually
incrementing is probably best for this scenario.
Yes that is correct, find the max and add 1, e.g. SELECT MAX(CategoryId) + 1 FROM Category
There is no need to mark every post you receive as an answer :)
To do it in linq, see this post http://stackoverflow.com/questions/3380265/avoiding-race-condition-when-manually-implementing-identity-like-increment-for-a
programercek
Participant
1510 Points
2244 Posts
How to increase the number + 1
Sep 28, 2010 12:19 PM|LINK
If it starts my IdNumber
3
How to get the last number and add +1?
Eg.
Column IdNumber
3
4
5
6
7
The next record is 8
Using LINQ
My code:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)] public bool AddZaposleni(int IdNumber, Guid UserId, string Priimek, string Ime, int Posta,string Kraj, string Delovno_mesto, int IdSifra) { // Create a new ProductRow instance Northwind.tbl_zaposleniDataTable tbl_zaposleni = new Northwind.tbl_zaposleniDataTable(); Northwind.tbl_zaposleniRow zaposleni = tbl_zaposleni.Newtbl_zaposleniRow(); // MembershipUser myObject = Membership.GetUser(); // Guid UserID = (Guid)myObject.ProviderUserKey; zaposleni.ID = IdNumber; zaposleni.IdSifra = IdSifra; zaposleni.UserId = UserId; zaposleni.Posta = Posta; if (Priimek == null) zaposleni.SetPriimekNull(); else zaposleni.Priimek = Priimek; if (Ime == null) zaposleni.SetImeNull(); else zaposleni.Ime = Ime; if (Kraj == null) zaposleni.SetKrajNull(); else zaposleni.Kraj = Kraj; if (Delovno_mesto == null) zaposleni.SetDelovno_mestoNull(); else zaposleni.Delovno_mesto = Delovno_mesto; // Add the new product tbl_zaposleni.Addtbl_zaposleniRow(zaposleni); int rowsAffected = Adapter.Update(tbl_zaposleni); // Return true if precisely one row was inserted, otherwise false return rowsAffected == 1; }Dharit Utpal...
Member
478 Points
107 Posts
Re: How to increase the number + 1
Sep 28, 2010 12:43 PM|LINK
In C#Dont Forget To Click On "ANSWER BUTTON" As It is helpful to others.
frez
Contributor
5418 Points
913 Posts
Re: How to increase the number + 1
Sep 28, 2010 12:48 PM|LINK
Why not set the database column to be an identity field then the database will increment the value for you and you want have to code around the problem that will occur if two users try to create a record at the same time?
programercek
Participant
1510 Points
2244 Posts
Re: How to increase the number + 1
Sep 28, 2010 12:53 PM|LINK
I can not. Because I want to start from number 3 onwards, or any that you write in your settings.
If the settings do I write the number 3, the next record to be number 4
If the settings do I write the number 10, the next record to be number 11
That is, by autocrement do that. Is this it?
Regards
frez
Contributor
5418 Points
913 Posts
Re: How to increase the number + 1
Sep 28, 2010 12:58 PM|LINK
You set the start number for an identity column in the column design so its not really appropriate for your problem. Just make sure you handle the error if two users try to insert the same id at the same time.
programercek
Participant
1510 Points
2244 Posts
Re: How to increase the number + 1
Sep 28, 2010 01:02 PM|LINK
Ok.
What if I use the same table for two different user?
Example:
the first user programercek
another user programercek2
Can the first user to specify which number goes on
and another user also?
The first user starts with the number 3
Another user is starting from the number 10 -
Is it possible to do with autoincrement?
thanks.
frez
Contributor
5418 Points
913 Posts
Re: How to increase the number + 1
Sep 28, 2010 01:11 PM|LINK
You dont want to be creating a different table for every user that would be a nightmare. You could store the starting number for a user and add that onto the number in the autoincrement field when displayng it, but I think your existing solution of manually incrementing is probably best for this scenario.
programercek
Participant
1510 Points
2244 Posts
Re: How to increase the number + 1
Sep 28, 2010 01:14 PM|LINK
If I understood correctly, in this case is best used, query = select max
and +1?
thanks.
frez
Contributor
5418 Points
913 Posts
Re: How to increase the number + 1
Sep 28, 2010 01:18 PM|LINK
Yes that is correct, find the max and add 1, e.g. SELECT MAX(CategoryId) + 1 FROM Category
There is no need to mark every post you receive as an answer :)
To do it in linq, see this post http://stackoverflow.com/questions/3380265/avoiding-race-condition-when-manually-implementing-identity-like-increment-for-a
programercek
Participant
1510 Points
2244 Posts
Re: How to increase the number + 1
Sep 28, 2010 05:59 PM|LINK
thanks