'Unable to Create/Add Rows in DataTable
I have deserialized a JSON string into a DataSet. I am trying to pull relevant rows from that DataSet and put it into a DataTable. I have followed several examples that I have found on SO, but I am getting an error of Cannot find column 0. I have confirmed that the JSON is deserialized correctly and that the data is I'm trying to pull is there.
Ideally, I'd prefer to have column names, but I received a similar error.
Here is my code
DataTable results = new();
DataRow workrow;
DataSet ds = JsonConvert.DeserializeObject<DataSet>(jsonData);
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
Console.WriteLine($"DataRow: {dr["listname"]}");
if (dr["listname"].ToString() == "ADJREASON")
{
Console.WriteLine($"Code: {dr["code"]}");
Console.WriteLine($"Desc: {dr["description"]}");
//workrow = results.NewRow();
//workrow["code"] = dr["code"];
//workrow["desc"] = dr["description"];
//results.Rows.Add(workrow);
workrow = results.NewRow();
workrow[0] = dr["code"];
workrow[1] = dr["description"];
results.Rows.Add(workrow);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
