473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Set primarykey to a datatable

Hi all

How can i set primarykey to a datatable.
This datatable is in a dataset i get from a backend system.

My datatable contains several fields, and i want to make the fields
"c-custno1", "c-custno2" and "c-class" my primary key.

How can this be done?
Thanks
Lars E
Apr 26 '06 #1
4 85880
DataTable.PrimaryKey
http://msdn2.microsoft.com/en-US/lib...rimarykey.aspx

Cheers,

Greg
"Lars E" <la***********@infocare.no> wrote in message
news:uD**************@TK2MSFTNGP02.phx.gbl...
Hi all

How can i set primarykey to a datatable.
This datatable is in a dataset i get from a backend system.

My datatable contains several fields, and i want to make the fields
"c-custno1", "c-custno2" and "c-class" my primary key.

How can this be done?
Thanks
Lars E

Apr 26 '06 #2
Example, multicolumn:

DataSet ds = new DataSet();
DataTable tbl;
//Create the Customers DataTable.
tbl = ds.Tables.Add("Customers");
tbl.Columns.Add("CustomerID", typeof(string));

tbl.PrimaryKey = new DataColumn[] {tbl.Columns["CustomerID"]};

//Create the Order Details DataTable.
tbl = ds.Tables.Add("Order Details");
tbl.Columns.Add("OrderID", typeof(int));
tbl.Columns.Add("ProductID", typeof(int));
tbl.PrimaryKey = new DataColumn[] {tbl.Columns["OrderID"],
tbl.Columns["ProductID"]};

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Lars E" wrote:
Hi all

How can i set primarykey to a datatable.
This datatable is in a dataset i get from a backend system.

My datatable contains several fields, and i want to make the fields
"c-custno1", "c-custno2" and "c-class" my primary key.

How can this be done?
Thanks
Lars E

Apr 26 '06 #3
Thanks.

I think i got the primary keys set.

But when i thies to use the datatable.row.find("primarykey") i get an error:

"Unable to cast object of type 'System.String[]' to type
'System.IConvertible'"
mycode:

//Run trough the table and populate the new fields
for(int i=0;i< dtCustInfo.Rows.Count;i++)
{
//build up the primarykey
String[][] custContKey = new String[][] {
new String[] { dtCustInfo.Rows[0]["c-custno1"].ToString() },
new String[] { dtCustInfo.Rows[0]["c-custno2"].ToString() },
new String[] { "a" }};
//my problem starts....
DataRow foundRow = dtCustCont.Rows.Find(custContKey); ->>>>>>> Error
if (foundRow != null)
{
MessageBox.Show("found one...");
//modifiy the 3 fields with data fra det dataset
//dtCustInfo.Columns.Add(
}
}

I don't have a clue on how to solve this. Please help?

Thanks.
Lars E.

"Greg Young [MVP]" <Dr*************@hotmail.com> skrev i melding
news:e5**************@TK2MSFTNGP03.phx.gbl...
DataTable.PrimaryKey
http://msdn2.microsoft.com/en-US/lib...rimarykey.aspx

Cheers,

Greg
"Lars E" <la***********@infocare.no> wrote in message
news:uD**************@TK2MSFTNGP02.phx.gbl...
Hi all

How can i set primarykey to a datatable.
This datatable is in a dataset i get from a backend system.

My datatable contains several fields, and i want to make the fields
"c-custno1", "c-custno2" and "c-class" my primary key.

How can this be done?
Thanks
Lars E


Apr 26 '06 #4
Hi again.

My solution for setting primary key.
I already have a datatable so i cant create column to this. My code now look
like this:

//make a datatable to use row.find() or datatable.select method
DataTable dtCustCont = new DataTable("CustCont");
dtCustCont = customers.Tables[1];
// set primary keys.
DataColumn[] keys = new DataColumn[3];
DataColumn column;
column = new DataColumn();
column = dtCustCont.Columns["cc-custnr1"];
keys[0] = column;
column = new DataColumn();
column = dtCustCont.Columns["cc-custnr2"];
keys[1] = column;
column = new DataColumn();
column = dtCustCont.Columns["cc-class"];
keys[2] = column;
// Set the PrimaryKeys property to the array.
dtCustCont.PrimaryKey = keys;

I now have problme with the dtCustCont.Select(......) so that my main issue
right now :-)

Thanks
Lars E

"Lars E" <la***********@infocare.no> skrev i melding
news:uD**************@TK2MSFTNGP02.phx.gbl...
Hi all

How can i set primarykey to a datatable.
This datatable is in a dataset i get from a backend system.

My datatable contains several fields, and i want to make the fields
"c-custno1", "c-custno2" and "c-class" my primary key.

How can this be done?
Thanks
Lars E

Apr 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
1
by: Mike | last post by:
I have an ASP.NET/VB app that updates values in a DataTable over the course of about 3 different pages. On the way out of the first of these pages, I explicitly build the DataTable from values in...
0
by: Chris Ericoli | last post by:
Hi, I am working with an 'in session' ado dataset with an asp.net application. My dataset is comprised of two tables, one of which maintains a few calculated datacolumns. For some reason these...
3
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something...
10
by: Bernie Yaeger | last post by:
I have a need to add a primary key to a dataset/datatable. How can this be done using a standard oledb data provider? Tx for any help.
10
by: dauphian | last post by:
Hello, I am new to .net and am trying to build a report application that queries 4 different tables based on a id, and I need to return them in the same table for easy viewing. Basically, I...
0
by: Indra Bisen | last post by:
Hi, I want to determine column names of Primary Key Columns of a SQL Server Table. To do this I've found following example in MSDN under PrimaryKey property of DataTable. I've add some lines of...
2
by: =?Utf-8?B?Sm9iIExvdA==?= | last post by:
How can I reconcile changes between two data table and create a subset of those changes. I have two data tables with same schema which gets populated from two different xml files. I want to get...
4
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, if i have an untyped datatable in my code how can i make it where when i reference the datatable variable i can say dt.FindByFieldKeyID(value as integer)? thanks, rodchar
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.