Friday, 17 September 2010

How to check a particular server exists and port is open

I had a requirement to relay emails via an SMTP server in one of my applications. This was not too hard to implement but a few users were not getting the server details correct and there was no way of checking if the details were valid when entering the details.

I used the code below to check if the server existed and the port was open by using the TcpClient class in the .Net framework.

How to check if a User Account exists using Directory Services

If you need to check a user account exists there is an easy method to call in .Net framework v3.5.

Code example below:

Uploading Files using FileUpload control in Update Panel in ASP.Net AJAX

If you have ever tried to use a FileUpload control inside an update panel you may find that it does not work or does not contain a file name. This is by design to avoid security holes where a file could be uploaded without you knowing. There is however a work around for this by making the upload button perform a full post back.

ASP Markup

Code Behind

Thursday, 16 September 2010

Ajax - 'sys' in undefined and the ASP.NET Ajax client-side framework failed to load

I had been struggling with this issue for a couple of days now. A small warning icon appears in Internet Exporer. Clicking on it shows a number of error messages.

1 - ASP.NET Ajax client-side framework failed to load
2 - 'sys' is undefined

I had checked the web.config file multiple times, all required sections were present and referencing the correct .net version dlls.

Finally after double checking absolutely everything I noticed that one of the handlers the web config was referencing did not have an extension mapping. This was the .axd extension. I added this in to IIS but the problem still occurred. Checking the handler mapping again I unchecked the 'Verify that the file exists' check box and retried the web page. This time it worked!

So in conclusion it turns out you do need to have the .axd extension mapping if it does not exist. And the check box to 'Verify the File Exists' must not be checked.

Thursday, 9 September 2010

Lock a workstation without Ctrl-Alt-Del

If you need to lock a work station but cant use Control Alt Delete (eg if you are connected remotely) you can enter the following command in Run.

rundll32.exe user32.dll, LockWorkStation

How to shrink a database using DBCC

Syntax for shrinking a database using DBCC on SQL server is as follows:


( database_name [ , target_percent ]



Is the name of the database to be shrunk. Database names must conform to the rules for identifiers. For more information, see Using Identifiers.


Is the desired percentage of free space left in the database file after the database has been shrunk.


Causes the freed file space to be retained in the database files. If not specified, the freed file space is released to the operating system.


Causes any unused space in the data files to be released to the operating system and shrinks the file to the last allocated extent, reducing the file size without moving any data. No attempt is made to relocate rows to unallocated pages. target_percent is ignored when TRUNCATEONLY is used.


This will shrink the database 'DemoData' leaving 10% free space.

How to make Ajax Calendar Extender cause postback on selection

Here is an example of how to make an Ajax Calendar extender caused a post back after a date is selected. In this example a label will be updated with the date that is selected in the calendar extender.


code behind:

Thursday, 2 September 2010

How to hide / show controls on a Master Page depending on the Content Page

I have recently had a problem where I needed to disable a button on a master page depending on which page is being displayed in one or more of the content place holders. Basically you can check the type or path of the page being displayed in the place holder.

This is easily achievable with the following code:

Simple Multi-threading Example

This example shows how to do processing on a separate thread so the thread that is handling drawing and updating the form does not get tied up on long running processes.

The program creates two threads. One which accepts a parameter and one which does not.

One thing to note in the code is the following snippet:

This ensures that a Cross Thread exception is not thrown. Basically a control cannot be updated by a thread other than the thread it was created on. As all controls are created on the main thread by the form a method must be Invoked on the control or the form rather than the alternate thread.

Full code listing below: