Monday, December 14, 2009

Placing HTML elements on top of the ModalPopupExtender control

If you want a HTML element to always be on top of the ModalPopupExtender control, give the element the CSS z-index value 99999998.

Find the mouse x, y position with JavaScript using jQuery

This is how you find the mouse x, y position with JavaScript using jQuery:

var _mouseX = 0;
var _mouseY = 0;

  _mouseX = e.pageX;
  _mouseY = e.pageY;

The variables _mouseX and _mouseY will at all times have the updated mouse coordinates.

Sunday, October 18, 2009

Test your WCF services using the WCF Test Client

Shipped with Microsoft Visual Studio is an application called the WCF Test Client (Start Menu > Open Visual Studio Command Prompt, type wcftestclient and press enter). This application can be used to test the methods/operations of any WCF service.

In the application, do File > Add Service, enter the service URL and click OK. You will now see the available service operations. Click on any operation, enter a value for each parameter and click Invoke. The result will be displayed in the response frame.

Monday, October 5, 2009

A recursive FindControl extension method

The System.Web.UI.Control class has a FindControl method for finding a control by its ID. This method searches for a given control in the first level child controls, and not in any child controls of a child. In order to do the latter, a custom method must be implemented. Below is a generic, recursive, extension method that does this:

public static class ControlExtension
 public static T FindControlRecursive(this Control container, string id)
   where T : Control
  if (container.HasControls())
   T foundControl = null;

   foreach (Control control in container.Controls)
    if (control.ID == id && control is T)
     foundControl = (T) control;
     foundControl = FindControlRecursive(control, id);

    if (foundControl != null)
     return foundControl;

  return null;


var control = Page.FindControlRecursive("TxtName");

Monday, August 3, 2009

CSV export to MS Excel, SYLK file error

I just received the following error when creating a CSV formatted file of some report data, and opening it in Microsoft Excel:
Excel has detected that 'xxx.xls', is a SYLK file. Either the file has errors or it is not a SYLK file format. Click OK to try to open the file in a different format.
After some research it turns out this problem occurs if the first two letters in the file content is ID (in uppercase). The only way to get rid of this error message is therefore to not use ID in uppercase (Id, id is ok), or change it to something else.

Monday, April 13, 2009

How to disable all the ASP.NET input controls within a container control

The following method will recursively disable all the input controls within a container control such as a User Control, PlaceHolder, Panel etc.:

public void DisableInputControls(Control container)
 foreach (Control control in container.Controls)
  if (control is TextBox
   || control is DropDownList
   || control is RadioButtonList
   || control is RadioButton
   || control is CheckBoxList
   || control is CheckBox
   || control is Button)
   (control as WebControl).Enabled = false;
  if (control.Controls.Count > 0)

Example usage:


Tuesday, March 24, 2009

How to generate the SHA-1 hash of a string using C#

The namespace System.Security.Cryptography contains classes for working with cryptography such as hashing. Within this namespace you will find the SHA1Managed class which we can utilize to generate the SHA-1 hash of a string. Below you will find a helper method using this class:

public static string GetSha1(string value)
 var data = Encoding.ASCII.GetBytes(value);
 var hashData = new SHA1Managed().ComputeHash(data);

 var hash = string.Empty;

 foreach (var b in hashData)
  hash += b.ToString("X2");

 return hash;

Friday, January 16, 2009

MS SQL-Server - How to select numeric rows from a varchar column

select ISNUMERIC('PostalCode123') -- Returns 0
select ISNUMERIC('123') -- Returns 1


select *
from Customer
where ISNUMERIC(PostalCode) = 1

Monday, January 12, 2009

Why you should not use the ADO.NET Entity Framework

Note: This post is about ADO.NET Entity Framework version 1.0. Newer versions of the framework might have been improved or solved any of the issues mentioned below.
For some days now I've been using the ADO.NET Entity Framework on a pet project of mine. My conclusion so far is that it's useless in most scenarios, and that I'm lucky to not use it on a real business application.

No support for the traditional n-layer architecture with a presentation, business and data-access layer

  • It's not possible to use your own business entities. If you have an existing codebase, then you have to rewrite all the code vs. if you used NHibernate, it's enough to modify the data-access layer to use NHibernate and map the result to your existing business entities using XML.
  • The only supported data source is database. In many applications today, data sources are both databases and services. For the services, you would need to define your own set of business entities and data-access code.
  • It is not possible to define the business-entities in a separate assembly. Thus the presentation layer such as a web-application, will need to reference the data-access layer in order to get access to the business entities.
  • The entity context instance has to be preserved for the entire request in order to allow for select, update and deletion of data. This needs to be taken care of in the business layer. One way of doing this is to add a reference to the System.Web assembly, and then use the request cache available through HttpContext.Current.Items. But you don't want a dependency on System.Web in your business layer. As Sean points out in his comment below, a way around this is to detach the object from the original context, attach the object to the new context, then call the extension method defined in Seans comment which will mark all of the properties on the attached object as modified. When SaveChanges() is called, the database will be properly updated. The same goes for deleting an object: detach, attach, DeleteObject(entity) and then finally SaveChanges().
  • If you want to delete data without using a stored procedure, then you first have to select the data to delete, iterate through each of the returned entities and call DeleteObject(entity), before you call SaveChanges(). Of course, this gives you really bad performance.

No support for calling stored procedures

When you add a stored procedure from the database to your data model, then the stored procedure will only be available through the entity context if it returns any of the model entities. The stored procedure will not be available if it returns nothing or any of the .NET primitive types such as string, int, bool etc.

Problems when updating the model from the database

When you make some database modifications after the model has been created, and you choose to update the model through the designer, then the end result is usually compiler exceptions. In my case, I always end up deleting the model and creating it from scratch, rather than fooling around in the designer generated file to find the bugs.