Issue with Update Row Activity for Data Table

Hi Experts,

I have an issue while using Update Row for data table. The table columns are ClientName (string) and TotalInvoice (integer). When debugging the robot, I got an error said:
Update Row Item: Table doesn’t have a primary key.

image

Here is the setting properties of Update row.
I tried to set “unique” in the data table for primary key related issues, however, I still got the same issue.

Please advice.

image

Thank you.

Hi @D_Okthree

You can set a primary key for your DataTable before using the “Update Row” activity.
yourDataTable.PrimaryKey = new DataColumn[] { yourDataTable.Columns["columnName"] }

Hi @supriya117 ,

Is primary key a requirement for using update row activity?
I thought it is applicable for any type of data table.

@D_Okthree

If you are giving for each and giving currentrow it is not…but if you are trying to find a row then it is

Cheers

@D_Okthree

This error typically appears when you are working with the “Update Row” activity within a DataTable that is part of a DataSet, and the DataTable does not have a primary key defined. So, the DataTable should have a primary key to uniquely identify rows for updating.

If you are working with a DataTable (not part of a DataSet) and still encounter this error, there might be another issue causing the problem. Check the properties you are using with the “Update Row” activity to ensure everything is correct.

@supriya117 ,

The thing is this is a simple data table and only consists of those two columns. We can’t use ClientName (people’s name) as a primary key. Also, I don’t use iteration to go through the whole data table. What do you think?

@Anil_G ,

How can we find a specific row for a desired value in a data table without using iteration?
Using code?

Thank you.

@D_Okthree

try to use select method instead of find
yourDataTable.Select("[ClientName] = 'SomeClientName'")

@supriya117 ,

Then how do we update it with that selection?
Since that’s the reason why I use update row activity.

Thank you.

@D_Okthree

You can try this

Dt.AsEnumerable.Where(function(x) x("ColumnName").ToStrinng.Equals("ABC")).First

This assumes definitely we have a match and on single

Cheers

@Anil_G ,

That means we will use invoke code activity?

@D_Okthree

Use for each

foreach (DataRow row in rowsToUpdate)
    row["colName"] = newValue

HI,

The following sample will help you.

dt.AsEnumerable.Where(Function(r) r(0).ToString="Bob").ToList.ForEach(Sub(r) 
    r(1)="50"
End Sub)

Sample
Sample20231003-4L.zip (2.8 KB)

Regards,

@D_Okthree

In the row field you can pass this…so that unique row is identified

If wanted to use invoke code

Then use below pass dt as in/out argument

Dt.AsEnumerable.Where(function(x) x("Column").ToString.Equals("abc")).ToList.ForEach(sub(r) r("NewCol") = "Valuetoupdate")

Cheers

@D_Okthree

As anil suggests use linq query in assign activity to update the row

yourDataTable.AsEnumerable().Where(Function(row) row.Field(Of String)("ClientName") = "SomeClientName").ToList().ForEach(Sub(row) row.SetField(Of Integer)("colToChange", newValue))

@Yoichi ,

I understand your suggestion.

Basically, I’m avoiding using for each since every client only has one row. I also want to update column “TotalInvoice” for a particular client, if it is found. Invoke code will not return any result/output.

Hi,

Did you set In/Out at Direction in arguments?

Regards,

@Yoichi ,

I don’t use argument since this is happen in the same process. I think argument or common variable are not significant in this matter?

Hi,

The above “argument” means InvokeCode’s one. It’s necessary to use arguments if pass data from/to codes inside InvokeCode.

Regards,

@Yoichi ,

I see. I understand your point regarding invoke code settings.
However, it doesn’t answer my questions from your recommendation:

  1. How can I update the “TotalInvoice” value from selection result based on particular “ClientName”? I don’t want to use iteration (for each) since every Client has only one row.

  2. How can I get the result/output from Invoke Code?