Search

Loading...

Feb 24, 2009

Sequence contains no element - LINQ

Recently using Aggregate function in LINQ I come across sequence contains no element error. After checking I found the list on which I am calling Aggregate function does not have any element!!!

Problem:
ERROR : Sequence contains no element, while using Aggregate function in LINQ

Lets see with example.

I have created AssignMe function which returns the collection of string, here is the function body.

private static string[] AssignMe()
{
return new string[] { };
}

For generating error AssignMe is returning nothing, now lets apply Aggregate function on this

string[] sList;
sList = AssignMe();
Console.Write(sList.Aggregate((first, second) => first + second));

This will throw InvalidOperationException => "Sequence contains no elements".

Lets change the AssignMe function little bit.

private static string[] AssignMe()
{
return new string[] { "NoException" };
}

This will work perfectly, and gives you output as NoException, don't get confused with NoException, Its returning result.

Solution:

We should check the item Count before applying Aggregate function, as we seen it's working well for single element. Lets put back our original AssignMe function and change the calling method.

string[] sList;
sList = AssignMe();

if (sList.Count() > 0)
Console.Write(sList.Aggregate((first, second) => first + second));


Working fine without an error :)

Conclusion:
While using Aggregate function or any other function like lamda expression or delegate; which accept argument then its better to check the Count of collection before applying such function.

Feb 19, 2009

File Upload not working with update panel

I was having problem while using File upload control which is inside the update panel and which was placed in user control. While doing post-back the value of PostedFile was blank, although page was properly valid.

Then I checked the Form property of Request object, the file upload control was missing in the AllKeys collection

We all know that if you want file uploads the form encode must be multipart/form-data. This is the reason why File Upload is not working properly inside Update Panel, lets understand this with following example.

Problem:

File upload is not working with update panel + asp.net

You are having two controls which needs to get display after one-another or post-back, first control does not have File upload and Second control does. Things to notice here is form tag will be reside either in ASPX page or Master page not in UserControl; and our File Upload is inside user control. Now when we load first control which does not have File Upload so the form will not encoded with multipart/form-data, now when you load second control which has the File Upload hence fort encode must be multipart/form-data, which is unfortunately not changing due to Update Panel.

Solution:

In problem definition we come to know that form must be encoded with multipart/form-data if we want to upload the file; solution is very simple, we just need to change encoded attribute of form with following single line!!!

Page.Form.Attributes.Add("enctype", "multipart/form-data");