LINQ: Sequence contains no elements

by Arnold Matusz 28 10 2008

The InvalidOperationException: Sequence contains no elements exception is thrown whenever you try to retreive an element from an empty sequence. This generally happens when you call First on an empty sequence. What I think about for this situation is that I'ld need a method which would return null if the sequence is empty and the first object (maybe the only object) in the sequence otherwise.

In this case the call of FirstOrDefault is recomended. Although after a few searches I've found something similar: we could Single or SingleOrDefault instead of First. Calling these methods is almost exactly what First does except ... this will generate another exception if the sequest contains more elements: InvalidOperationException: Sequence contains more than one element. But the convenient factor is that it quietly returns the single element in a sequence, null of the sequence is empty and the above mentioned InvalidOperationException: Sequence contains more than one element. » Continue reading ...

What's hot in ASP.NET 3.5 SP1

by Arnold Matusz 22 10 2008

First of all it's worth mentioning that Microsoft brought enhancements to the ASP.NET AJAX Extensions. Now we can use a History control which allows us to use the back button to navigate back to a state before the last async postback happened.

Then there is Dynamic Data which is a framework that discoveres the data model and determines the UI which is best suited, dynamically, during runtime. This sounds weird but it can be of big help! Ex: When you try to display a DateTime value in a GridView, you can use a <asp:DynamicField ... /> which will automatically print your DateTime value nicely formatted. To achieve this with the Normal <asp:BoundField ... /> you needed to set HtmlEncode="false" and DataFormatString="{0:D}". If your DataField is a simple string, this will render a LiteralControl. Not only do these work in Read-Only mode, this also happens in Edit mode which can big of big help. » Continue reading ...

ValidationSummary displayed multiple times in UpdatePanel

by Arnold Matusz 14 10 2008

Restricting the user from leaving certain fields blank is often mandatory, because we don't want to have empty fields in our database, or we may want the user to specify exactly how many products he wishes to order. Asp.net comes in really good here with the Validation Controls. For my example the RequiredFieldValidator is perfectly suited because it displays a little error message for the control it validates.

All is fine until you have 40-50 fields that have to be filled out. In this case it's of good practice to use the ValidatioSummary control which summarises all the error messages from the ValidationGroup and shows them either in text on the page or as a Javascript alert().  » Continue reading ...

Styling a TreeNode with CssClass

by Arnold Matusz 9 10 2008

Web development with Asp.net is very fast. We have drag and drop controls with out of the box functionality. This concept of prebuild controls is of huge advantage when it comes to rapid application development, saving us allot of time ... but there is one major drawback. We are normally limited to what the controls offer in: rendering, functionality, etc.

Microsoft controls normally render table based designs, which are very difficult to style. If you want to override this you can achieve it using CssFriendlyControlAdapters. It's the same case for the TreeView Control but the table based rendering is probably not the thing that bothers me the most! » Continue reading ...

Colorful parallelism

by Arnold Matusz 7 10 2008

I thought of rail lines as two infinitely parallel lines, that they don't ever intersect. And still when looking at this digital photo it's somehow a paradox.
My maths teacher told us that two parallel lines meet in infinity.

Colorful parallelism - optical illusion
» Continue reading ...

Server.Transfer in an UpdatePanel

by Arnold Matusz 6 10 2008

While writing a short piece of code for my global exception handling post I figured out a nice fact. What I wanted to achieve is to have redirection to a custom error page which displays exception details. This can be easily done by using Response.Redirect() (to ex: ~/exception.aspx"), but this will completely lead to another page. This is not usefull if I'm interested in the actual URL where the exception has been thrown. (ex: ~/products.aspx?catid=4&pageid=7 ..."). By using Response.Redirect() you end up with "/exception.aspx" as the URL in the browser.

An obvious solution at first sight is ... we need to use Server.Transfer() which won't create a new Context (i.e.: the url in the browser will stay the same but we show the contents of a completely new page). And of course this is all fine ... until you need to use this functionality in an ASP.NET AJAX enabled website in an UpdatePanel.

By calling Server.Transfer() during an asynchronous postback the following alert will be displayed:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '
<!DOCTYPE html P'.

The reason why you can't use a Server.Transfer() in an UpdatePanel is more and more obvious when you think about what AJAX is. We only request small pieces of markup which replace little portions of a page, the whole point of AJAX is to avoid downloading the whole page again. This means that when triggering an async postback, the PageRequestManager expects to receive some small portion of HTML, but because the Server.Transfer() call was made the actual response is the markup for a completely new/different page (which begins with the DOCTYPE definition, this is what you can see at the end of the exception message).

If you decide to use a Server.Transfer() in an UpdatePanel you can avoid this exception by setting your Trigger to a PostBackTrigger. (i.e. The control which fires the method where Server.Transfer() is called won't cause an async postback!).
» Continue reading ...

ASP.net global exception handling and custom error page

by Arnold Matusz 1 10 2008

This is for all those who have allready seen the yellow screen of death. If you are an ASP.net web developer this might have happened to you a few times. It is very helpful as it displays most of the information you need regarding the exception source but in a few cases this might not be enough.

For example, if an exception is thrown within an UpdatePanel the yellow screen of death won't display, instead you'll get a JavaScript alert("") from Sys.WebForms.PageRequestManagerServerErrorException which displays the Exception.Message. (In this case you don't know which object generated the exception and find yourself in a situation where you miss the yellow screen of death). Global exception handling comes in good here, it is a good place to save exception details in a database table or to show it to the developer in a nicely formatted manner. » Continue reading ...

About Arnold Matusz

Arnold Matusz

My name is Arnold Matusz. I'm a web developer specialized in .NET technologies with a passion for photography and cars.

View Arnold Matusz's profile on LinkedIn

Who's amung us