Strange behavior when getting key

Viewed 99

So I have this line:

string theWhereClause = " levels = '" + LevelItems[SelectedLevel] + 
                        "' AND areas = '" + AreaItems[SelectedArea] + "'";

The AreaItems is a Dictionary<string,string> and the SelectedArea has the value "Statistics and Probability" for some reason, when debugging, it breaks on this line:

AreaItems[SelectedArea] 

and throws this error:

The given key was not present in the dictionary.

This is what is inside the AreaItems[]:

[0] {[All, All]}
[1] {[Number, Number]}
[2] {[Shape Space and Measures, Shape Space and Measures]}
[3] {[Statistics and Probability, Statistics and Probability]}
[4] {[Algebra, Algebra]}
[5] {[Other, Other]}

However, if I hard code the string so:

AreaItems["Statistics and Probability"] it works perfectly!

What I have tried:

AreaItems[SelectedArea].Trim() 

Thinking their were some hidden characters, this still didn't work.

I tried:

SelectedArea = "Shape Space and Measures";
AreaItems[SelectedArea] 

Thinking that spacing in the variable would be the reason, I was wrong.. It works with that value in the variable.

I did an IF statement to check if their was any difference between the 2:

if(SelectedArea == "Statistics and Probability")
{

}

and it falls through the IF statement, so that returns true.

Then lastly something that did work is:

string temp = AreaItems[SelectedArea];
string theWhereClause = " levels = '" + LevelItems[SelectedLevel] + 
                        "' AND areas = '" + temp + "'";

All I did this time is store the AreaItems value in a different string (temp) and placed that string within the theWhereClause and it worked!

Although this works, i'm still intrigued as to why the first time it didn't!

What difference is grabbing the value from AreaItems[SelectedArea] and string temp = AreaItems[SelectedArea] is there to cause such a strange behavior?

Additional code to help you guys:

So to call this method I am using AJAX which sends the values to this method:

    [HttpPost]
    public ActionResult SearchForWorksheets(string level, string area)
    {
        Models.FreeMathsWorksheets freeWorkSheets = new Models.FreeMathsWorksheets();
        freeWorkSheets.SelectedLevel = level;

        // When debugging, at this point the area is "Statistics and Probability"
        freeWorkSheets.SelectedArea = area

        freeWorkSheets.BuildWhereClause();
        List<Models.SearchMathWorksheetsList> WorksheetList = new List<Models.SearchMathWorksheetsList>();
        WorksheetList = freeWorkSheets.ConvertDataTableToList();
        return Json(WorksheetList);
     }

Now because of this line Models.FreeMathsWorksheets freeWorkSheets = new Models.FreeMathsWorksheets(); the constructor which calls methods to build the dictionarys gets called within the FreeMathsWorksheets class:

public class FreeMathsWorksheets
{      

   public string SelectedLevel { get; set; }

   public string SelectedArea { get; set; }

    public FreeMathsWorksheets()
    {
        PopulateAreaDropdown();
    }

    public void PopulateAreaDropdown()
    {
        string theSql = "SELECT * FROM HL_FreeWorksheetAreas";
        IDataAccess da = new DataAccess();
        DataTable dt = da.GetData(theSql);
        AreaItems = new Dictionary<string, string>();

        // Default add an all value
        AreaItems.Add("All", "All");

        foreach (DataRow row in dt.Rows)
        {
            AreaItems.Add(row["area"].ToString(), row["area"].ToString());
        }
    }    

    public void BuildWhereClause()
    {
        string theWhereClause = " levels = '" + LevelItems[SelectedLevel] + "' AND areas = '" + AreaItems[SelectedArea] + "'";
    }
}
0 Answers
Related