Click here to Skip to main content
11,417,972 members (42,355 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#5
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 22-Sep-11 19:21pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Tech Code Freak at 23-Sep-11 0:45am
   
Good links!
My 5up!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can make the primary key as Identity column. Itz possible via create table statement or GUI table designer.

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
  Permalink  
v2
Comments
Tech Code Freak at 23-Sep-11 1:02am
   
Good Solution!
My 5up!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
0 Sascha Lefévre 340
1 Abhinav S 339
2 OriginalGriff 315
3 Sergey Alexandrovich Kryukov 245
4 deepankarbhatnagar 121
0 Sergey Alexandrovich Kryukov 9,345
1 OriginalGriff 7,712
2 Maciej Los 3,758
3 Abhinav S 3,601
4 Sascha Lefévre 3,198


Advertise | Privacy | Mobile
Web03 | 2.8.150427.4 | Last Updated 23 Sep 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100