This question already has an answer here:
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();
}
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;
.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();
}
selectedItem != null
as true
. - EnigmativityKeyValuePair<int, string>
is never null
. KeyValuePair<int, string>
is a value-type. - Enigmativity
selectedItem.First()
will give that error when selectedItem is empty. - MixxiphoidcolumnList.Where(cl => cl.Value == defColumnList[index])
yields an empty sequence which you then callFirst()
on. - spenderSelectedItem
is going to produce a squence of items. When you check to see if its not null the sequence isnt null and callingFirst
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