0

I have dynamic grid row textboxes, i add values and save, it gets serialized into json format and gets saved in database, now when i have to populate the grid(when i refresh)the saved data needs to be displayed on the textboxes. My code:

  foreach (GridViewRow row in gv.Rows)
            {
                TextBox txtKey = (TextBox)e.Row.FindControl("txtKey");
                TextBox txtValue  = ((TextBox)row.FindControl("txtValue");


                string JsonDeserialize = dtTerminalLocalConfig.Rows[0]["Settings"].ToString();



                List<KeyValuePair<string, string>> configurations = new List<KeyValuePair<string, string>>();

                configurations = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(JsonDeserialize);

                configurations.Add(new KeyValuePair<string, string>(txtKey.Text, txtValue.text));

                txtKey.Text  = configurations.SingleOrDefault(K => K.Key == LocalConfigurations.Key).Value;

               txtValue.Text = configurations.SingleOrDefault(K => K.Key == LocalConfigurations.Value).Value;

            }

The 'configuration' has deserialized json strings and is assigned to txtboxes, but the textbox are giving null when debugged

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Any chance you could [edit] your question to share a [mcve] ([as text, not as a screen shot](https://meta.stackoverflow.com/a/285557)) showing the JSON value of `JsonDeserialize` that reproduces the problem? From [ask]: *Help others reproduce the problem... if your problem is with code you've written, you should include some...Include just enough code to allow others to reproduce the problem.* You have included the code, we just need to see the JSON to understand why it is not deserializing your ` List>`. – dbc Jan 20 '23 at 16:00

1 Answers1

0

This is more of a code review.

So, here are a few questions and answers:

Why is this chunk inside the foreach loop? Do you genuinely need to do this for every row, or just once?

string JsonDeserialize = dtTerminalLocalConfig.Rows[0]["Settings"].ToString();
List<KeyValuePair<string, string>> configurations = new List<KeyValuePair<string, string>>();
configurations = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(JsonDeserialize);

(You can roll those last two statements into a single line btw:

List<KeyValuePair<string, string>> configurations = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(JsonDeserialize);

What is this line doing?:

configurations.Add(new KeyValuePair<string, string>(txtKey.Text, txtValue.text));

Why in your Linq statements are you only getting the key == LocalConfigurations.Key? That looks like it exists outside of the foreach loop, and so would populate each row with the same key/value pair.

Why are you getting a KeyValuePair twice with Linq?

Should the first line be setting txtKey.Text to .Value?

Should the second line be K => K.Key == LocalConfigurations.Value ? Why Not:

KeyValuePair<string,string> kvp = configurations.SingleOrDefault(K => K.Key == LocalConfigurations.Key);
txtKey.Text  = kvp.Key;
txtValue.Text = kvp.Value;

Although if these are genuinely KeyValuePairs (i.e. the keys are unique), Why not deserialise to a Dictionary<string,string>? This would allow you to do something like:

string JsonDeserialize = dtTerminalLocalConfig.Rows[0]["Settings"].ToString();
Dictionary<string, string>> configurations = JsonConvert.DeserializeObject<Dictionary<string, string>>>(JsonDeserialize);
    foreach (GridViewRow row in gv.Rows)
    {
        TextBox txtKey = (TextBox)e.Row.FindControl("txtKey");
        TextBox txtValue  = (TextBox)row.FindControl("txtValue");
        if(!configurations.TryGetValue(key), out string value) continue;
        txtKey.Text  = key;
        txtValue.Text = value;
    }

Hopefully this helps somewhat.

Andy Wynn
  • 1,171
  • 7
  • 11