Update a column value c#

Hello I am trying to change a column value from c# application

i make the following query

string query = @“{”“query”“:”“mutation{change_column_value(item_id:456191942, board_id: 454686247, column_id: ““numbers””, value:”“”“123456"”“”){ name}}“”}";

But it seems something in my escaping of the string is not right and I get bad request!
I am begging for some help! I have been struggling with that for two days.
if someone has c# code to help me I will be so grateful.

Hey Tanya!

We need to know a little more to get to the bottom of this.

  1. Are you using the helper class that @grt-s shared to make this request?

  2. Have you successfully made any API requests via your C# code? Can you show an example of this, as well as the exact methods you’re using to make this request?

Thanks! :crystal_ball:

Hello, thank you so much for your fast reply.

  1. Yes I have used the helper class - even I wasted time to make my own client service and some modifications @grt-s class but no matter if i use his helper class or my service - the result is the same.

  2. of course I have made some queries that were successful - 1. his query 2. I successfully get all the items and column values of my board.

But when It comes to modifying data

          string query = @"{""query"": ""mutation {create_item(board_id: 454686247, item_name: \""adding works\"") { id} }"" }";
        MondayHelper helepr = new MondayHelper();
        helepr.QueryMondayApiV2(query);

this works - but you see how strangely I have escaped the item_name

and as long as I try to add column values -

   string addingvaluesquery = @"{""query"": ""mutation {create_item(board_id: 454686247, item_name: \""changing  values item!\"", column_values: ""{\""text\"": \""changeee\""}"") { id} }"" }";

or modify columns data 0- cannot do anything. It just gives me bad request and the problem is that i don’t get any information further to see where is my mistake. My company wants to work with monday - but I should find a way to populate with data.
Thank you so much in advance I have wasted two days already on this

Hey Tanya! Thanks for sending that context over. Glad you’ve gotten a create_item mutation to work. We can focus on the column values part now :slight_smile:

First off, let me start by saying I’m not a C# expert. But I do know our API inside out so I’m sure we can put our heads together to solve this!

  1. Our API takes column values as string-ified JSON, which sometimes includes strings inside it.
  2. If we try to explicitly pass these strings-inside-strings, you will sometimes run into issues with bad requests. It’s beneficial to instead let a JSON library do the hard work :muscle: I’ve heard Json.NET is a good one to use.
  3. I would also recommend using GraphQL variables to pass your arguments. This way, you don’t have to escape anything and instead you can pass your arguments as part of a JSON object.

Here’s a programming pattern that I’ve found helpful when making requests like this work in an environment that does not play nicely. It’s kind of a mix of psuedocode and C#, I’ll let you edit it to work in your environment :slight_smile:

// store query as a string, declaring the item name and column values as GraphQL variables
string myQuery = "mutation ($name: String!, $values: JSON!) {create_item(board_id: 454686247, item_name: $name, column_values:$values) { id} }" 

// store column values as a dictionary
string vals = @{"text" : "This is a text value!"}

// convert column values into JSON string 
string vals_json = JSON.SerializeObject(vals)

// create variables dictionary using the item name 
string myVariables = @{"name" : "Item name", "values" : vals_json}

// construct payload for actual API request
string query = @{""query"" : myQuery, ""variables"" : myVariables}

What do you think? Does that make sense? Let me know :+1:

Hey @taniamm2002 I replied to your issue on my C# demo on GitHub but thought I’d post here too in case anyone else walks down this path! Here’s a link to the issue.

This is pretty ugly code I know with all of the string escaping! If you need to scale it up for several different mutations it is probably much easier to use a JSON library, use GraphQL variables, and build up the query as @dipro suggests. If I get some time at some point I’ll have a go at a nicer version and add it to the example repo.

All that said the current one seems to work for me so it might get you unstuck for now :slight_smile:

1 Like

Thank you thank you so much I tried to follow the upper pattern and I have achieved creating an element. Again problem with the column values. but I am sure I will have a result.

Hey @taniamm2002!

For the column values, you sometimes need to serialize the column data as JSON before putting them in the variables object, but in other situations the data gets passed fine without this step. For example, in my Python code I need to do double-serializing, but JS handles it with no issue.

If you keep having issues with item creation, you can also simplify this call by first creating the item and then looping through the columns and updating them one at a time. @lucy.parry’s amazing example walks through this :slight_smile:

Good luck!!