473,605 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 85913
DataTable.Prima ryKey
http://msdn2.microsoft.com/en-US/lib...rimarykey.aspx

Cheers,

Greg
"Lars E" <la***********@ infocare.no> wrote in message
news:uD******** ******@TK2MSFTN GP02.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.f ind("primarykey ") i get an error:

"Unable to cast object of type 'System.String[]' to type
'System.IConver tible'"
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(custContK ey); ->>>>>>> Error
if (foundRow != null)
{
MessageBox.Show ("found one...");
//modifiy the 3 fields with data fra det dataset
//dtCustInfo.Colu mns.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******** ******@TK2MSFTN GP03.phx.gbl...
DataTable.Prima ryKey
http://msdn2.microsoft.com/en-US/lib...rimarykey.aspx

Cheers,

Greg
"Lars E" <la***********@ infocare.no> wrote in message
news:uD******** ******@TK2MSFTN GP02.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.selec t method
DataTable dtCustCont = new DataTable("Cust Cont");
dtCustCont = customers.Table s[1];
// set primary keys.
DataColumn[] keys = new DataColumn[3];
DataColumn column;
column = new DataColumn();
column = dtCustCont.Colu mns["cc-custnr1"];
keys[0] = column;
column = new DataColumn();
column = dtCustCont.Colu mns["cc-custnr2"];
keys[1] = column;
column = new DataColumn();
column = dtCustCont.Colu mns["cc-class"];
keys[2] = column;
// Set the PrimaryKeys property to the array.
dtCustCont.Prim aryKey = keys;

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

Thanks
Lars E

"Lars E" <la***********@ infocare.no> skrev i melding
news:uD******** ******@TK2MSFTN GP02.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
1387
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/1/2004 4000.00 40.00 4040.00 1/2/2004 1000.00 10.00 1010.00 1/2/2204 2000.00 20.00 2020.00 1/3/2004 1500.00 15.00 1515.00 I want my resultant DataTable to show in DataGrid as
1
2965
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 a DataGrid, and set the PrimaryKey of the DataTable to be the first cell in the grid (which is a UserID value). I then store the DataTable in a session object, from which it is retrieved for subsequent pages. All this seems to be working fine. ...
0
3136
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 datacolumns do not trigger their expression when other columns from which the expressions are derived are updated. Below is a basic example of what I am doing. User enters values into an asp.net form and clicks a button. Retrieve dataset from...
3
3181
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 missing from the code? Thanks - Jon Private Sub MakeParentTable() ' Create a new DataTable. Dim myDataTable As Datatable = New Datatable("ParentTable")
10
27839
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
6522
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 have one querie that grabs all of the id's I need for the other 4 queries, but I am not sure how to get them into a DataTable or DataSet, or if that is the best way to do this. Seperately the queries all work with no problems.
0
1060
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 code to initialize table object and rest is as it dim table as new datatable() dim adp as new sqlclient.sqldataadapter("Select * from emp",cn) adp.fill (table) Dim columns() As DataColumn
2
2486
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 hold of missing & changed rows in first table from second table. Tables have ID as primary key. Thanks
4
2006
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
7934
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8425
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1271
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.