Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...

I am Using a "Depositor ID",in my "Depositor Details" form which is connected to "depositor details" SQL table....

I am using a "Primary key" for Dep_ID, I hope all you know what is "Primary key"

and my problem is

I have to "autogenerate" the depositor ID... I have used several ways for generating the number from Depositor details SQl table such as,

1.)count(Dep_ID)+1(in select Query) and

2.)isnull(max(Dep_ID),0)+1

but no use of it..

can anyone please suggest me with C# coding
Posted

Why don't you just set it as an auto-incrementing column? The database will auto-increment it for you when you insert a new record. You can have the stored proc return the generated ID (or the inserted record which includes the auto-incremented ID.
 
Share this answer
 
Comments
Espen Harlinn 6-Jan-11 6:16am    
5+ Go for the inserted record approach - otherwise you might run into concurrency issues ...
Just set the table to do this for you. You shouldn't be generating primary keys manually

In SQL Server this is achieved by setting the 'Identity' field to true
 
Share this answer
 
Comments
Hari 2 6-Jan-11 6:29am    
Hi Morley...
I have used the 'identity' as you said in SQL...
But the exception occurs with the message "the column Dep_ID must be of integer datatype"..

Since I have used "Depositor ID",the ID,can not only be the number... It includes Text also(say Dep001) so I have to use "Varchar" datatype...


Any idea dude!!!!!!
Dylan Morley 6-Jan-11 6:36am    
I'd say rethink your primary key!

You primary key should be numeric. Set it to be an integer and let the database autoincrement the data and take care of assigning identity to records

Something like 'Dep001' is more for display purposes, which you can easily take care of with a bit of SQL when returning data to your client application

Otherwise, in your unique primary field, you're going to be storing and repeating data like

Dep001
Dep002
Dep003
Dep004
Dep005

You might have done a bit of work already, but best to get the designs right at the start. Correct table design is like the foundations to a building, very important - get them right and you'll save yourself lots of problems further down the line.
If you are on SQL Server use an identity column
If you are on Oracle use a sequence to generate your keys

Whatever you do it has to happen in an atomic unit of work, as understood by the database. The above proposals are the easies way to accomplish this ...

Regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900