Category Archives: .NET

HttpException: File does not exist

Since some time I happened to get a nasty exception in the global.asax Application_Error method. Exception details are below:

Type : System.Web.HttpException
Message : File does not exist.
Source : System.Web
ErrorCode : -2147467259
Data : System.Collections.ListDictionaryInternal
TargetSite : Void ProcessRequestInternal(System.Web.HttpContext)
Stack Trace :
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)
at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

After some hard time trying to resolve this bug, I finally figured out what the problem was. The problem related to this exception is an HttpRequest to a non-existing source, not being the main request, but something like an image or iframe source. In my case, it was an iframe source loaded with a non-existing document.

To resolve this exception at your place, add the following to your Watch window: ((HttpApplication)sender).Context.Request.Url and set a breakpoint on the Application_Error method. This way, the exception will clarify itself a lot (it definitely did for me).

Set MasterPage (.master) title from a web form (.aspx)

Lots of people complain about not being able to change the master page’s title using ASP.NET 2.0 and up. A commonly seen tutorial is to use the following piece of code:

((HtmlTitle) Page.Master.FindControl("lblTitle")).Text =
    "Set some kind of title";

In most of the cases you need to change the master page’s title, you do not have to bother about this. Just use the correct page stadium and set the page’s title. The only page event to be able to do this is Page_PreInit (since master page and content page are still separate ‘pages’ in this stadium). The following will work (if you have AutoEventWireup enabled ;) ):

protected void Page_PreInit(object sender, EventArgs e)
{
    Title = "Set some kind of title";
}

IsDateTime in .NET for both ticks and string representation

Some while ago I developed a Settings system, where the SettingValue type should be checked. The DateTime.TryParse function did not do all the work I wanted it to do, so I thought ‘why not develop my own workaround’ :) ? It is posted below…

/// <summary>
/// The IsDateTime function returns whether the given expression is a DateTime or not.
/// </summary>
/// <param name="expression">Expression to check for being DateTime.</param>
/// <returns>Boolean</returns>
/// <remarks>
/// The DateTime expression can be given as ticks or as normal DateTime representation.
/// </remarks>
public static bool IsDateTime(object expression) {
if(expression == null) return false;Bert Loedeman

string stringExpression = System.Convert.ToString(expression);
if(String.IsNullOrEmpty(stringExpression)) return false;

// DateTime can be specified in ticks. In that case the string should be convertible
// to a long (System.Int64) and fit in a specific range.
long retLong;
if(Int64.TryParse(stringExpression, out retLong)) {
// Ticks: 0 = DateTime.MinValue, 0x2bca2875f4373fff = DateTime.MaxValue
if((retLong < DateTime.MinValue.Ticks) || (retLong > DateTime.MaxValue.Ticks)) return false;
return true;
}

DateTime retDateTime;
return DateTime.TryParse(stringExpression, out retDateTime);
}

Search and replace with Regular Expressions in Visual Studio .NET (2005)

Today I had to search and replace a text with a little variation in it in a very large project. This text looked like ‘if<zero or more spaces>(Null.IsNull(<object name>.<variable name>))’. I had to replace it with: ‘if (!<object name>.<variable name> > 0))’. This turns out to be quite a job, especially when you are not very experienced with Microsoft’s way of dealing with regular expressions. Finally I discovered a way to do the job. The expressions used for it are the following:

Search string: if[ ]*\(Null.IsNull\({:c+.:c+}\)\)
Replacement string: if (!(\1 > 0))

Note that the Regular Expression used to search does not entirely look likea normal RegEx. Microsoft seems to use their own kind of RegEx for search and replace. Important is that {} is not used to quantify like in RegEx (for instance: zo{1} matches ‘zo’ but not ‘zoo’), but to generate a tagged expression, which can be used in the replacement string by giving in \x whereby x represents a 1-base number (1st replacement = \1 and so on).

Hope I have been of some help for anybody out there :) . Please do not be concerned about posting a comment/question. I’ll do what I can to answer you soon!

Bert