Set primarykey to a datatable

L

Lars E

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
 
G

Guest

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
 
L

Lars E

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.
 
L

Lars E

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top