Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting DataTable primary key [duplicate]

Tags:

c#

objDataTable.Columns.Add("Student Id", typeof(int));
objDataTable.PrimaryKey = "Student Id";

am already tried many ways but i can't

in c# how to set primary key to this line , friends please help

thanks in Advance

like image 815
gowtham v Avatar asked Aug 05 '16 06:08

gowtham v


People also ask

How to set DataTable primary key?

The primary key for a table is set by specifying an array of DataColumn objects from the table. The following example illustrates creating a primary key based on two columns: // set the primary key based on two columns in the DataTable DataTable dt = new DataTable("MyTable"); dt. Columns.

What is a primary key c#?

A database table commonly has a column or group of columns that uniquely identifies each row in the table. This identifying column or group of columns is called the primary key.

What is a primary key in a dataset?

A primary key is the column or columns that contain values that uniquely identify each row in a table. A database table must have a primary key for Optim to insert, update, restore, or delete data from a database table.


1 Answers

Try this out :

DataTable objDataTable = new DataTable();
objDataTable.Columns.Add("Student Id", typeof(int));
objDataTable.PrimaryKey = new DataColumn[] { objDataTable.Columns["Student Id"] };

The property PrimaryKey is of Type: System.Data.DataColumn[] not string.

like image 90
Sadique Avatar answered Sep 21 '22 16:09

Sadique