0

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.

8
  • The following may be helpful: learn.microsoft.com/en-us/dotnet/framework/data/adonet/… , learn.microsoft.com/en-us/dotnet/api/… , and learn.microsoft.com/en-us/dotnet/api/… Commented Mar 30, 2022 at 13:58
  • @user9938 Ok, but why did the first debug code work, and then the next foreach bcause an error. Looks like I can do ReasonCodeTable.PrimaryKey = new DataColumn[] { "Reason Code","LocationID" }; Commented Mar 30, 2022 at 19:32
  • Please add some sample data to your post. Also the following statement doesn't make sense: What I'm trying to do is to update the ReasonCodeTablefrom the R64CodeTable by matching the "code" to the "Reason Code" and if it has R64 in it The rest of the paragraph could use a re-write, as well, to make things more clear. Commented Mar 30, 2022 at 20:37
  • A DataTable is 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 a DataTable - you could very well have used a List of a class. (ex: List<MyClass1>). Commented Mar 30, 2022 at 20:43
  • @user9938 Updated the question. I changed how I built the DataTable so I could create a PrimaryKey. I don't know why it still says it isn't there. Commented Mar 30, 2022 at 20:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.