Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i'm bigginer at c#.please somebody help me!
i want to put an id as a primary key in my database and fill it automatically but in c# there is an error with my code,how i can do that?
plz help me!
Posted

Hi
In DataBase

On creation of table you specify Id as PrimaryKey and set Identity(1,1) then it takes care of filling in sequence

http://msdn.microsoft.com/en-us/library/aa933196(v=SQL.80).aspx[^]

On saving of entity you dont need to pass Id from application..

In c#

I dont think you need to do any thing...If you have a requirement in application to identify which property refers to PrimaryKey in datatable..then you can mark property is a Primary by means of Attribute..

more to know from

http://msdn.microsoft.com/en-us/library/z0w1kczw.aspx[^]
 
Share this answer
 
Comments
Tech Code Freak 23-Sep-11 0:45am    
Good links!
My 5up!
You can make the primary key as Identity column. Itz possible via create table statement or GUI table designer.

SQL
CREATE TABLE myTableName ( myPrimary INT IDENTITY(1, 1), ... )


It automatically inserts the value for myPrimary key starting from 1 and increment of 1. You can change the start and increment points in IDENTITY(1, 1) parameters
 
Share this answer
 
v2
Comments
Tech Code Freak 23-Sep-11 1:02am    
Good Solution!
My 5up!
DataTableName.PrimaryKey = new DataColumn() {DataTableName.Columns("ID")} u can set this way in C# Data table

as far database concern u add the the primay key in your Sql Table with this way

ALTER TABLE Customer ADD PRIMARY KEY (CustomerId);

if you are creating new SQl Table

CREATE TABLE MyTable (cola INT IDENTITY(1, 1) PRIMARY KEY ,Col2 varchar)

IDENTITY always generates new Id at the time of insertion of any record
 
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