Update a specific data row in a data table

I have a question I am trying to update a column value in a particular row.
This is the code:


UpdateSpecificRow.xaml (6.0 KB)
I am encountering an error at this activity:Assign(1)
Where I assign the variable:
OneRowFromDataTable a DataRow object:

dt_Input.Select(“ORDER_NUM=1-48676792953”).FirstOrDefault

The error I receive:
Assign(1): Cannot perform '=' operation on System.String and System.Int64.
I don’t understand is why am I encountering this error when there is no datatype of String or Integer involved in the assign activity.
Kindly assist. Thank you.

Hi @RobinsonFrancis

Can you please let us know based on what condition you are updating a column?

Best Regards.

Hi @RobinsonFrancis

If you want to update a column in datatable then you need to know the extract row index (starting index from 0)

If you that row index means, you can make use of this below expression and put inside assign activity

youDataTable.Rows(0).Item(“columnName”) = value needs to changed

Regards
Robin

Okay, done. I now have a new problem, it is the second activity.
Where I assign

OneRowFromDataTable(0) = “Lolol”

The error I am getting is :slight_smile:

Source: Assign(2)

Message: Cannot create an L-value from the given expression with property 'set_Item' because the target object is null.

Exception Type: System.NullReferenceException

System.NullReferenceException: Cannot create an L-value from the given expression with property 'set_Item' because the target object is null.   at System.Activities.ExpressionUtilities.IndexerLocationFactory`1.IndexerLocation.set_Value(T value)
   at System.Activities.Location`1.ReferenceLocation.set_Value(T value)

Can you help me on this as well? Thank you.

Put it on the second assign activity

Based on my understanding OneRowFromDataTable is must be a datatable variable. If it is you have to enter your vb expression like this OneRowFromDataTable.Rows(0).Item("Column name you want modify / column index) = “Lolol”

For example I have a datatable called DT and it has 2 rows
Emp Name Age Salary Company Code
01 Robi 23 10000. 1
02 Navi 25 25630 4

If I need to update the first row in the salary column means. I have to enter below expression on my code
DT.Rows(0).Item(“Salary”) = “20000”

After updation My datatable will look like
Emp Name Age Salary Company Code
01 Robi 23 20000 1
02 Navi 25 25630 4

Regards
Robin

The condition is when the ORDER_NUM field for that particular row is 1-48676792953.
dt_Input.Select(“ORDER_NUM=1-48676792953”).FirstOrDefault

OneRowFromDataTable is actually System.Data.DataRow.

@RobinsonFrancis

Well, in that case, the following query will get you the result:

io_dt.AsEnumerable.ToList.ForEach(Sub(row)
row("Name") = If(row("Order_Num").ToString.Equals("1-48676792953"),"Updated Name",row("Name").ToString)
End Sub)

Output:

Hope this helps,
Best Regards.

1 Like

Hi @RobinsonFrancis ,

I have updated your workflow and attached below,
UpdateSpecificRow.xaml (13.6 KB)

Hope this may help you :slight_smile:

2 Likes

I don’t know but I got this error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Column 'ORDER_STATUS' does not belong to table DataTable.   at System.Data.DataRow.GetDataColumn(String columnName)
   at System.Data.DataRow.get_Item(String columnName)
   at UiPathCodeRunner_365675f5c1ae40b6a5aa5e93ed4438a7._Closure$__._Lambda$__0-0(DataRow row)
   

However when I checked the Excel spreadsheet that, the Column ‘ORDER_STATUS’ does exist in the Excel and therefore it should exist in the DataTable that it had been read into.
UpdateSpecificRow_2.xaml (7.1 KB)

I made a small modification to your LINQ statement, min looks like this:

io_dt.AsEnumerable.ToList.ForEach(Sub(row)
row("ORDER_STATUS") = If(row("Order_Num").ToString.Equals("1-48676792953"),"Updated Name",row("ORDER_STATUS").ToString)
End Sub)

Thank you.

1 Like

@RobinsonFrancis,

As the column name was ‘name’ in my excel file, I used row(“name”). Glad you figured our row(“ORDER_STATUS”) as per your requirement.

Best Regards.

Thanks, I have tried it on my own. I made a small modification to your LINQ statement, mine looks like this:

io_dt.AsEnumerable.ToList.ForEach(Sub(row)
row("ORDER_STATUS") = If(row("Order_Num").ToString.Equals("1-48676792953"),"Updated Name",row("ORDER_STATUS").ToString)
End Sub)
[UpdateSpecificRow_2.xaml|attachment](upload://f4fb9ggpoE2yHmQDR1pEdj0XFcv.xaml) (7.1 KB)

However, when I ran it, I got this error
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Column 'ORDER_STATUS' does not belong to table DataTable. at System.Data.DataRow.GetDataColumn(String columnName) at System.Data.DataRow.get_Item(String columnName) at UiPathCodeRunner_365675f5c1ae40b6a5aa5e93ed4438a7._Closure$__._Lambda$__0-0(DataRow row)
This is my LINQ statement:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.