I am updating a DataTable, and when I do it says Table doesn't have a primary key. I changed how I created the table, and added a PrimaryKey, so what's going on?
I create the table, add columns, and add rows with data, and some blank values. The DEBUG code right after it's populated works fine.
I then want to populate some of those blank values, but when I do I get the error.
Here is my code:
DataRow workrow;
DataTable ReasonCodeTable = new();
DataColumn dc1 = new();
dc1.ColumnName = "Reason Code";
dc1.DataType = typeof(string);
DataColumn dc2 = new();
dc2.ColumnName = "LocationID";
dc2.DataType = typeof(string);
DataColumn dc3 = new();
dc3.ColumnName = "Location";
dc3.DataType = typeof(string);
DataColumn dc4 = new();
dc4.ColumnName = "Issue";
dc4.DataType = typeof(string);
DataColumn dc5 = new();
dc5.ColumnName = "Description";
dc5.DataType = typeof(string);
DataColumn dc6 = new();
dc6.ColumnName = "Who Edited";
dc6.DataType = typeof(string);
DataColumn dc7 = new();
dc7.ColumnName = "Last Edit (UTC)";
dc7.DataType = typeof(string);
ReasonCodeTable.Columns.Add(dc1);
ReasonCodeTable.Columns.Add(dc2);
ReasonCodeTable.Columns.Add(dc3);
ReasonCodeTable.Columns.Add(dc4);
ReasonCodeTable.Columns.Add(dc5);
ReasonCodeTable.Columns.Add(dc6);
ReasonCodeTable.Columns.Add(dc7);
ReasonCodeTable.PrimaryKey = new DataColumn[] { dc1, dc2 };
// Builds ReasonCodeTable
foreach (string code in codeList)
{
foreach (KeyValuePair<String, String> loc in Locations)
{
workrow = ReasonCodeTable.NewRow();
workrow["Reason Code"] = code;
workrow["LocationID"] = loc.Key;
workrow["Location"] = loc.Value;
workrow["Issue"] = "";
workrow["Description"] = "";
workrow["Who Edited"] = "";
workrow["Last Edit (UTC)"] = "";
ReasonCodeTable.Rows.Add(workrow);
}
}
/*** DEBUG BEGIN ***/
foreach(DataRow dr in ReasonCodeTable.Rows)
{
for (int cnt = 0; cnt < ReasonCodeTable.Columns.Count; cnt++)
Console.WriteLine($"{ReasonCodeTable.Columns[cnt].ColumnName}: {dr[cnt]}");
Console.WriteLine($"");
}
/*** DEBUG END ***/
foreach (DataRow _rcdr in ReasonCodeTable.Rows)
{
foreach (DataRow _dr in p21CodeTable.Rows)
{
if ( (p21CodeTable.Rows.Find(_rcdr["Reason Code"]) != null) && (p21CodeTable.Rows.Find("P21") != null) )
{
_rcdr["Desciption"] = _dr["description"];
_rcdr["Who Edited"] = _dr["editwho"];
_rcdr["Last Edit (UTC)"] = _dr["editdate"];
}
}
}
/*** DEBUG BEGIN ***/
foreach(DataRow dr in ReasonCodeTable.Rows)
{
for (int cnt = 0; cnt < ReasonCodeTable.Columns.Count; cnt++)
Console.WriteLine($"{ReasonCodeTable.Columns[cnt].ColumnName}: {dr[cnt]}");
Console.WriteLine($"");
}
/*** DEBUG END ***/
R64CodeTable:
company: IBM
code: 10
description: Inventory Count
editdate: 2020-05-19
editwho: User12
company: IBM
code: 11
description: Data Error
editdate: 2021-06-28
editwho: User12
company: IBM
code: 12
description: Warehouse Error
editdate: 2021-08-19
editwho: User06
ReasonCodeTable after initial population:
Reason Code: 10
LocationID: R64
Location: R64
Issue:
Description:
Who Edited:
Last Edit (UTC):
Reason Code: 10
LocationID: WH1
Location: Chicago
Issue:
Description:
Who Edited:
Last Edit (UTC):
Reason Code: 11
LocationID: R64
Location: R64
Issue:
Description:
Who Edited:
Last Edit (UTC):
ReasonCodeTable how it should look after update:
Reason Code: 10
LocationID: R64
Location: R64
Issue:
Description: Inventory Count
Who Edited: User12
Last Edit (UTC): 2020-05-19
Reason Code: 10
LocationID: WH1
Location: Chicago
Issue:
Description:
Who Edited:
Last Edit (UTC):
Reason Code: 11
LocationID: R64
Location: R64
Issue:
Description: Data Error
Who Edited: User12
Last Edit (UTC): 2021-06-28
I don't understand what's going on with the primary key.
ReasonCodeTable.PrimaryKey = new DataColumn[] { "Reason Code","LocationID" };DataTableis usually used when a database is the source for the data (although this isn't a requirement). It seems that you aren't using any syntax that would benefit from aDataTable- you could very well have used aListof a class. (ex:List<MyClass1>).