Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Help me wit the auto generation codes.....
Posted

You don't create a Primary Key Id from a value in a textbox. All you do is insert all the other fields you need to create a new record and leave the ID field alone. If you're database is setup correctly, it'll generate an ID for the record itself and then you can get that ID back and show it if you want. Typically, you don't let users see or edit Primary Key fields.
 
Share this answer
 
Comments
David A. Gray 27-Apr-14 23:43pm    
To elaborate a bit, write a stored procedure that creates the new record from field values passed into it as arguments. The SP returns a single value, which is the key. You insert the value into the ID field when you post the form back, to notify the user that the record was saved.

The stored procedure is executed as a NonQuery, allowing you to capture its return value into a variable that receives it through the Execute NonQuery ADO method.

This is the approach I've used in several ASP.NET applications and at least a couple of desktop applications, all of which had SQL Server data bases behind them.
if you want to create a new id for your customer field in sql database in windows form, you can use random class for this.

for example

string CustomerId="";
Random rnd=new Random();
CustomerId=rnd.Next(10,1000);
 
Share this answer
 
Comments
Dave Kreskowiak 27-Apr-14 17:19pm    
That's a horrible idea. What if two copes of the code generate the same ID number?

The correct way to do this is to have the database generate the ID, not your code.
_Starbug_ 27-Apr-14 17:33pm    
sorry. you are right. i do not read your question carefully.you can set for your customer id field identity increment and identity seed. sorry again.

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