.NET, Technology

How to auto-register types in Castle Windsor

This is how you can use Castle Windsor’s fluent interface to auto-register all the types within one or more assemblies:

private static void RegisterTypes(WindsorContainer container)
{
        var assemblies = GetAssembliesToRegister();

        foreach (var assembly in assemblies)
        {
            container.Register(
                    AllTypes.Pick().FromAssemblyNamed(assembly)
                    .If(x => x.IsPublic)
                    .If(x => x.GetInterfaces().Length > 0)
                    .Configure(x => x.LifeStyle.Transient)
                    .WithService.FirstInterface()
                    );
        }
}
private static List<string> GetAssembliesToRegister()
{
        var assemblies = new List<string>();

        assemblies.Add("DemoApp.DataAccess");
	assemblies.Add("DemoApp.BusinessLogic");

        return assemblies;
}
SQL Server, Technology

Paging in SQL Server 2005/2008

In order to do paging in SQL Server 2005 or later, we need to use a Common Table Expression (CTE). Below is a practical example.

Regular SQL query without paging:

select *
from Person.Contact
order by FirstName, LastName;

Query as a CTE without paging:

with Persons as
(
	select *
	from Person.Contact
)
select *
from Persons
order by FirstName, LastName;

Query as a CTE with paging, retrieving row number 41 to 60:

with PagedPersons as
(
	select *,
		row_number() over (order by FirstName, LastName) as RowNumber
	from Person.Contact
)
select *
from PagedPersons
where RowNumber between 41 and 60
order by FirstName, LastName;

Convert between PageNumber/RowsPerPage and FirstRow/LastRow:

declare @PageNumber int
declare @RowsPerPage int
declare @FirstRow int
declare @LastRow int

set @PageNumber = 3
set @RowsPerPage = 20

set @LastRow = @PageNumber * @RowsPerPage
set @FirstRow = @LastRow - @RowsPerPage + 1;

with PagedPersons as
(
	select *,
		row_number() over (order by FirstName, LastName) as RowNumber
	from Person.Contact
)
select *
from PagedPersons
where RowNumber between @FirstRow and @LastRow
order by FirstName, LastName;
Other software, Technology

How to run WordPress locally against a live database

If you try to run a local version of WordPress against a live database, you will be redirected to the live URL. The reason for this is that the URL is defined in the database (wp_options table). In order to work around this, you can add the local URL to the /wp-config.php file:

define('WP_SITEURL', 'http://localhost:81');
define('WP_HOME', 'http://localhost:81');
Other software, Technology

How to get Fiddler to work under Windows 7

If you try to trace your web applications using Fiddler with default settings under Windows 7, you will get the following error in your browser:

[Fiddler] Connection to localhost. failed.
Exception Text: No connection could be made because the target machine actively refused it ::1:80

This can be fixed by disabling IPv6 in Fiddler:

1. Go to Fiddler, then Tools > Fiddler Options.
2. Uncheck "Enable IPv6 (if available)".

Tracing should now work.

Other software, Technology

How to get Fiddler to trace localhost traffic

Fiddler is a great tool for tracing the HTTP traffic for your web applications. However, if you try to trace http://localhost/ you will see that it does not work. This can be fixed by either using the machine name or adding a dot at the end of the localhost address:

http://mycomputername/

or

http://localhost./
ASP.NET, Technology

A generic FindControl extension method

In order to clean up and make our code more readable, generics and extension methods comes in very handy. In ASP.NET, the Control class has a FindControl method which is used as follows:

((TextBox) Page.FindControl("TxtName")).Text = "Some name";

Since this method returns a Control type, we must cast it to it’s actual type before we can access any of it’s members. A way to improve this code, is to create a new generic FindControl method. Since all the controls in ASP.NET such as Page, Button, TextBox, inherits from the Control class, we can make this method an extension method. By doing this, our method can be accessed the same way as the non-generic FindControl method:

Page.FindControl<TextBox>("TxtName").Text = "Some name";

This code is tidier and easier to read. Here is the code for the extension method:

public static class ControlExtensions
{
	public static T FindControl<T>(this System.Web.UI.Control control, string id)
		where T : System.Web.UI.Control
	{
		return (T) control.FindControl(id);
	}
}
ASP.NET, Technology

Always wire your events in the code behind, NOT in the aspx/ascx template file

With ASP.NET you can wire your events in the aspx/ascx template file or in the code behind file. If you wire your events in the template file, you will not get compile time support. This means that if a method is renamed or removed without updating the template file, the compiler will not throw an error. However, if the event is wired in the code behind, an error will be thrown. Events should therefore always be wired in the code behind.

Template file event wiring - not recommended:

<asp:Button runat="server" ID="BtnSave" OnClick="Save" Text="Save" />

Code behind event wiring - recommended:

protected override void OnInit(EventArgs e)
{
	base.OnInit(e);

	BtnSave.Click += new EventHandler(Save);
}
ASP.NET, Technology

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.

JavaScript, Technology

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;

jQuery(document).ready(function()
{
	$().mousemove(function(e)
	{
		_mouseX = e.pageX;
		_mouseY = e.pageY;
	});
})

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

Technology, WCF

Test your WCF services using the WCF Test Client

Shipped with Microsoft Visual Studio is an application called the WCF Test Client (C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\WcfTestClient.exe). 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.

WCF Test Client

Older Posts »