0

I have a C# dictionary that contains several Key Value pairs. One of the keys is Category and may have a value of "7. Test\Training\Malfunction\Accidental" . When writing the values to my database I want to ignore the records that have this category.

I have tried using this code:

string t = dict["data"][i]["categories"][0].ToString();
if (t != "7. Test\\Training\\Malfunction\\Accidental")
{
    doSomething();
}

But I get this error message Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.

How can I handle this value that will not cast to a string?

I really do not believe this is the same issue addressed as the duplicate. The error is not that the index number is out of range. I am sure of this because just above the If statement I am having a problem with is this code:

updatedRecord = (dict["data"][i]["evidenceId"]);
string checkId = (dict["data"][i]["evidenceId"]);
    **ADD my New
    if (IDChecked(checkId)) //this is a new record add it
       { 
          TaserData Data = new TaserData();
         Data.EvidenceId = (dict["data"][i]["evidenceId"]);
          Data.IdExternal = (dict["data"][i]["idExternal"]) == null ? "" :(dict["data"][i]["idExternal"]);
         ---ADD several more items to the database---

What I am trying to do is add }

All of this is wrapped in a long for each loop that generates the index numbers. Did not want to drop all of the code on the post.

Perry
  • 1,277
  • 2
  • 17
  • 39

0 Answers0