-1

I have two Dictionaries. One with a list of columns from an Excel sheet and a list of defined columns. I wanna know if the defined columns exists in the sheet. If they exists then they will selected in a chosen dropdownlist.

In the row dropdownList.SelectedValue = selectedItem.First().Key; I get sometimes an errormessage Sequence contains no elements. I thought that I had coded safety. What do I forget?

... the command ...

SetDataSource(import.ColumnList, import.DefColumnList, ddlSomeColumn, SomeEnum.Zipcode);

... and then the calling method ...

    private void SetDataSource(Dictionary<int, string> columnList, Dictionary<int, string> defColumnList, DropDownList dropdownList, SomeEnum item)
    {
        int index = (int)item;
        dropdownList.BeginUpdate();
        dropdownList.ValueMember = "Key";
        dropdownList.DisplayMember = "Value";
        dropdownList.DataSource = columnList;
        if (defColumnList.ContainsKey(index) && defColumnList[index].Length > 0)
        {
            var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]);
            if (selectedItem != null)
                dropdownList.SelectedValue = selectedItem.First().Key;
        } 
        dropdownList.EndUpdate();
    }
user1531040
  • 2,143
  • 6
  • 28
  • 48
  • 3
    `selectedItem.First()` will give that error when selectedItem is empty. – Mixxiphoid Jun 06 '17 at 10:52
  • 3
    This happens when `columnList.Where(cl => cl.Value == defColumnList[index])` yields an empty sequence which you then call `First()` on. – spender Jun 06 '17 at 10:52
  • `SelectedItem` is going to produce a squence of items. When you check to see if its not null the sequence isnt null and calling `First` will cause this error. Its best to change your check to something like this var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]); if (selectedItem != null && selectedItem.Any()) dropdownList.SelectedValue = selectedItem.First().Key; – Bad Dub Jun 06 '17 at 10:56
  • I know that when First() is empty you get an error. But I was looking for something like this: if (selectedItem.Any()). And it works... – user1531040 Jun 06 '17 at 10:59
  • I do understand now: The title of my question was wrong... I have changed this. – user1531040 Jun 06 '17 at 11:32

2 Answers2

2

The meaning of this error is that selectedItem is having no element while you are trying to access first element which is not possible.

Instead of checking nullability you should check is there any element inside the collection and then should execute First method on collection.

var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]);
if (selectedItem.Any())
    dropdownList.SelectedValue = selectedItem.First().Key;
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
1

.Where() operator returns an enumeration, and not a single element. So, your selectedeItem!=null condition always returns true. Change your code to this:

private void SetDataSource(Dictionary<int, string> columnList, Dictionary<int, string> defColumnList, DropDownList dropdownList, SomeEnum item)
    {
        int index = (int)item;
        dropdownList.BeginUpdate();
        dropdownList.ValueMember = "Key";
        dropdownList.DisplayMember = "Value";
        dropdownList.DataSource = columnList;
        if (defColumnList.ContainsKey(index) && defColumnList[index].Length > 0)
        {
            var selectedItem = columnList.FirstOrDefault(cl => cl.Value == defColumnList[index]);
            if (selectedItem != null)
                dropdownList.SelectedValue = selectedItem.Key;
        } 
        dropdownList.EndUpdate();
    }
Volma
  • 1,305
  • 9
  • 17