Posts

Clearly Defined

I was talking with a friend today, an older friend who is cantankerous, funny, and often sarcastically insightful. We have some things in common, such as our choice of careers, and we both play guitar, so I enjoy talking with him quite a bit. Today, the topic of some old movies came up. I can't remember how. One of the movies he mentioned was The Great Race by Blake Edwards, and starring Tony Curtis and Jack Lemmon. I've never seen it, but my friend was telling me about how the hero and villain were so clearly defined in the film, with the hero dressed impeccably in a white suit, and the villain in black with a wiry moustache and top hat. The way he described it, it seemed cliche, which was part of the appeal. And it made me think of my own life lately, and how I wished for things to be that well defined, to have that same contrast. There is no black and white in my life. At least, not lately, not recently. There are various shades of gray, with the circumstances, people, and ...

"$.ajax is not a function" Error When Using jQuery and Darren Johnstone's ASP.NET Upload/Download Module v2

Lately I've been working with an excellent file upload library for ASP.NET written by a guy named Darren Johnstone. This library allows you to upload files and see a modal dialog and progress bar which indicates the percentage of the file upload having been completed. It doesn't use Flash (or Silverlight for that matter), just good ol' server-side .NET and some AJAX on the client-side. However, it also killed some existing jQuery code I had in place to make some AJAX calls to WCF services. I found I was now getting the following script error: $.ajax is not a function My workaround was to change my jQuery code to not use the $ abbreviation. So, in other words, I changed $.ajax to jQuery.ajax -- then it started working again. I'm not sure what it is about the upload control that affected this, but the workaround is manageable. Now I'll just need to explain to everyone why my code uses the long version instead of the shorter one anytime someone says to me "Hey, ...

"Sequence contains more than one element" Error Using LINQ

I wrote some code today utilizing LINQ to retrieve values from an XML document, and received the following error: "Sequence contains more than one element" I was confused because the code I wrote was very similar to other code I've written, which works fine. Turns out the error was caused because the results of my LINQ query contained more than one object, and I was using the SingleOrDefault() method -- I wanted to retrieve only the first matching object of the results. However, what I should have been doing was using the FirstOrDefault() method. The "Sequence contains more than one element" error occurs when you use SingleOrDefault() and your results contain more than one element. In situations like this, use FirstOrDefault() instead.

How to Get the Physical Path of an ASP.NET Application

This is one of those things I do infrequently enough that every time I need to, I ask myself "Now how do I do that again?". So I'm posting this here not only to help anyone else who needs to do this, but also to be able to easily remind myself how next time I need to. To get the physical path of an ASP.NET application, you can use: HttpContext.Current.Request.PhysicalApplicationPath

ASP.NET Cached Items Being Removed

Lately some of the items I've been placing in cache in ASP.NET have been being removed. When I assigned a callback method to trap these removals, I saw that the reason for removal was "underused" -- ASP.NET was removing these items to free up memory because they weren't being used often. To prevent this from happening, use the following value for the priority parameter of Cache.Add() : CacheItemPriority.NotRemovable

HttpRuntime.Cache vs. HttpContext.Current.Cache in ASP.NET

Lately I'd been using the static Current property of the HttpContext object in ASP.NET to access it's Cache property and cache information in a web app. I was adding these cache entries with dependencies, so that when certain files were modified, the cache would trigger a callback function to reload. However, when testing this, I ran into an unforseen complication: when I modified a file I had specified as a dependancy, and the app tried to add the new data to the cache using HttpContext.Current.Cache , it couldn't because there was no current HttpContext . The solution was to use HttpRuntime.Cache instead. This, like HttpContext.Current.Cache , represents the cache of the entire app, but does not require a reference to a current HttpContext . Also, I've heard it's a little bit faster.

"Object reference not set to an instance of an object" Exception When Using A DetailsView Control In ASP.NET

I ran into an exception recently when developing a website in ASP.NET which made use of a DetailsView control bound to an ADO.NET Entity Data Model as its data source (EntityDataSource). The complete exception message was: [NullReferenceException: Object reference not set to an instance of an object.] System.Web.UI.WebControls.EntityDataSourceView.ConvertWCProperty(IDictionary values, Dictionary`2 convertedValues, List`1 visitedProperties, PropertyDescriptor pd, ParameterCollection referenceParameters, Dictionary`2& exceptions) +35 System.Web.UI.WebControls.EntityDataSourceView.ConvertProperties(IDictionary values, PropertyDescriptorCollection propertyDescriptors, ParameterCollection referenceParameters) +216 System.Web.UI.WebControls.EntityDataSourceView.CreateEntityForInsert(EntityDataSourceWrapper entityWrapper, IDictionary values, ParameterCollection insertParameters) +49 System.Web.UI.WebControls.EntityDataSourceView.ExecuteInsert(IDictionary values) +239 System...