Skip to main content
Seasoned ⭐️⭐️⭐️
Apr 1, 2024 5:57 AM UTC
Solved

C# SDK Update data in RAW

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}");
}

 

Practitioner ⭐️⭐️⭐️
Apr 2, 2024 6:05 AM UTC

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.

7 replies

Mithila Jayalath
Expert ⭐️⭐️⭐️⭐️
Expert ⭐️⭐️⭐️⭐️
Apr 1, 2024 11:08 AM UTC

@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}");
}

 

Seasoned ⭐️⭐️⭐️
Apr 1, 2024 11:12 AM UTC

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

Mithila Jayalath
Expert ⭐️⭐️⭐️⭐️
Expert ⭐️⭐️⭐️⭐️
Apr 1, 2024 11:54 AM UTC

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

Seasoned ⭐️⭐️⭐️
Apr 1, 2024 11:59 AM UTC

@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();

Seasoned ⭐️⭐️⭐️
Apr 1, 2024 12:13 PM UTC

After update all the columns shrink into one.

 

Practitioner ⭐️⭐️⭐️
Apr 2, 2024 6:05 AM UTC

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.

Seasoned ⭐️⭐️⭐️
Apr 4, 2024 8:54 AM UTC

@Einar Omang Thanks it worked.