Solved

C# SDK Update data in RAW

  • 1 April 2024
  • 7 replies
  • 64 views

Badge +1

Hi All,

I am trying to update data in RAW table. I am using below code but after executing it either column values are not modified or sometimes all the columns shrink under one columns as ({} columns) and column data is also not updated in that case.

 

var row = confModel.RawRow as RawRow<Dictionary<string, JsonElement>>;
if (row == null)
{
    return;
}

var currentDate = DateTime.Now;
var utcDate = currentDate.ToUniversalTime().ToString("u").Replace(" ", "T");
row.Columns[UpdatedBy] = GetJsonElement(JsonValue.Create("SYSTEM"));
row.Columns[SimulationStatus] = GetJsonElement(JsonValue.Create(status.ToString()));
row.Columns[UpdatedDate] = GetJsonElement(JsonValue.Create(utcDate));
var rawRowCreateColl = new List<RawRowCreate<RawRow<Dictionary<string, JsonElement>>>>();
var rawRowCreate = new RawRowCreate<RawRow<Dictionary<string, JsonElement>>>();
rawRowCreate.Key = confModel.RowKey;
rawRowCreate.Columns = row;
rawRowCreateColl.Add(rawRowCreate);

try
{
    SdkClient.Raw.CreateRowsAsync(database, table, rawRowCreateColl);
}
catch (Exception e)
{
    Console.WriteLine($"Error: {e.Message}");
}

 

icon

Best answer by Einar Omang 2 April 2024, 08:05

View original

7 replies

Userlevel 2
Badge

@Adarsh Dhiman  can you change the try catch method as below and try again?

try
{
await SdkClient.Raw.CreateRowsAsync(database, table, rawRowCreateColl);
Console.WriteLine("Data updated successfully.");
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}

 

Badge +1

@Mithila Jayalath External My method is not async so I am not using await on CreateRowsAsync.

Userlevel 2
Badge

@Adarsh Dhiman isn’t the CreateRowsAsync method is an async mothod in the Cognite SDK to create rows in a table? 

Badge +1

@Mithila Jayalath External In case if you are writing non async code you can call async code without await and can use Wait on that task. I missed that wait. Let me try with it.

var createRowTask = SdkClient.Raw.CreateRowsAsync(database, table, rawRowCreateColl);
createRowTask.Wait();

Badge +1

After update all the columns shrink into one.

 

Userlevel 2

You are explicitly creating a `RawRowCreate<RawRow<Dictionary<string, JsonElement>>>`, in other words, a raw row where the columns are a full `RawRow`. You want to make it a `RawRowCreate<Dictionary<string, JsonElement>>`, and move only the `Columns` part of the retrieved row over.

Badge +1

@Einar Omang Thanks it worked.

Reply