Thursday 23 December 2010

How to determine the services running under a SVCHOST.EXE process using Task List

1. Click on the Start button and then click on the Run menu command.
2. In the Open: field type cmd and press enter.
3. You will now be presented with a console window. At the command prompt type tasklist /svc /fi "imagename eq svchost.exe" and press the enter key. You will see a list of the processes on your computer as well as the services that a SVCHOST.EXE process is managing. This can be seen in the image below.

Wednesday 22 December 2010

The difference between '<%#' and '<%=' in ASP.NET

I have been trying to work out the difference between "<%#" and "<%=" when writing markup for an ASP.Net site. Sometimes the = sign worked, and sometimes the # sign works but switching them usually sorted out the problem but I was never really sure why.

I found a post on Dan Crevier's Blog that explained the difference very well.

To sum it up it works as follows:
  • The "<%=" expressions are evaluated at render time
  • The "<%#" expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
  • "<%#" expressions can be used as properties in server-side controls. "<%"= expressions cannot.

Monday 13 December 2010

SQL - Saving Changes is not permitted when altering table


If you are trying to edit the data type in a table in SQL server and you get a message saying "Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created" then you will be pleased to know there is a setting for this that seems to be on as default in a new install.


If you open the settings screen from Tools > Options then go down to the 'Designers' node you should see an option called "Prevent saving changes that require table re-creation". If you remove the check mark from this option then you should be able to save as normal.

Friday 10 December 2010

Programmer Joke

Thursday 9 December 2010

SQL script to get databases missing a full backup within the past 24 Hours

Here is an SQL script to get databases missing a full backup within the past 24 Hours


I found this over at SQL Tips

Script to get Most recent SQL Backup date for each database

Here is a script to get the most recent backup date for each database in SQL server


I found this over at SQL Tips

How to find all database backups in the last week

Here is a useful script to find all backups done on all databases over the last 7 days. The number of days can be changed if necessary:

I found this script over at SQL Tips

Friday 19 November 2010

Crystal Reports - Could not load file or assembly 'System.Web.Extensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies

I was upgrading an old ASP project that used crystal reports and came across the following error:

Could not load file or assembly 'System.Web.Extensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.

This was caused by the project being upgraded to use .net 3.5. As i wanted to use .net 3.5 i could not downgrade it to version 2.0. Luckily I found some information on the web for assembly binding redirection that is configured in the web.config. Making the changes below solved the problem.

old web.config entry

new web.config entry


This is basically telling ASP to redirect calls to any System.Web.Extensions dll from version 1 to version 3 to go to version 3.5. Before it was only redirecting .net 1 and .net 1.1 calls to .net 3.5.

Monday 15 November 2010

Visual Studio Color Theme Editor Crashes Visual Studio after applying a theme

I have been playing around with the Visual Studio Color Theme Editor extension and come across a problem where Visual Studio crashes every time I try and load a theme file to apply.
Its fairly obvious when its going to crash as after the theme has loaded into the list and the apply button is clicked is does not disable the apply button afterwards.

Ended up running process monitor to find the files or registry keys this is using and found

Deleting this key fixed the problem with visual studio crashing so I can now load new themes.

NOTE: This also removed all of the other custom saved themes

Friday 12 November 2010

Using LINQ to Sort Data

Usually to sort objects with a Sort() method on a list the objects need to have a Comparer class that is used to compare the object types. This is fine but gets old fast when having to implement this multiple times for different object types.

This is where Linq comes in

Take a look at this method below: I cant sort in SQL as the properties I need to sort on don’t exist in the table.

Friday 15 October 2010

Execute multiple select statements to a DataSet in one command

I recently had the need to improve performance on a large web app. Looking in SQL profiler I could see that there were a lot of SQL commands happening that would be more efficient if I could do multiple queries in one command. Turns out it is possible and not that hard!

Here is an abstract of the data class that handles reading data from the database:


And here is how you would call it:


The only thing you have to remember is to specify the table names so you can access them from the dataset. Ideally it would be nice to read the table names from the SQL command itself but this is for later.

Tuesday 12 October 2010

Popup Control Extenders in a grid view

If you have seen this error before then you may have been dabbling in javascript and Ajax. I wanted to hover over an icon on each row of a gridview to show more details.

For this I added an image button to the item template of the gridview and attached a Hover Menu Extender from the Ajax control toolkit. This worked great but performance became an issue when there were more records being displayed in the grid view. Eventually Internet explorer will show the above error message.

After a day of struggling I cam up with the following solution;

Hooking up a new Hover menu extender on each row looked like the source of the problem. Taking an idea from the Script Manager i decided I should have some kind of manager control I can add to the page to manage the scripts for each gridview item rather than seperately on each row.

I have an IconViewer control which contains a few icons that can be hovered, and an IconViewerManager control to store most of the JavaScript.

The part to note in the IconViewer code behind is the BindIcons method, this sets the images for my buttons but more important adds JavaScript to the onMouseOver and onMouseLeave events. The JavaScript is calling    
scripts that exists in the IconViewerManager control.

The IconViewerManager control contains the popup control extenders, this means that only 2 will exist on the page instead of one or two for every row in a gridview.

Its the JavaScript functions here that do the real work. When the icon is hovered over the Show method will be called. This sets the dynamic context key which is passed to the JavaScript method from the IconViewer control. This is to dynamically load the data from a page method added to the page that contains the gridview.

the set_parentElement method repositions the popup on the control that is also passed from the IconViewer control, this is so the popup is moved to the icon that is being hovered.

finally popupbehaviour.show displays the popup on screen.

IconViewer source:
Markup:

Code Behind:


IconViewerManager source:
Markup:

Saturday 9 October 2010

Ajax ComboBox Object reference not set to an instance of an object.

I was getting the following error after completing an operation in a web application and trying to click a link to enter a new Invoice on the same page.

This turned out to be due to one of the combo boxes trying to load its postback data (because I was navigating to the same page). The reason the error was being thrown is because I disabled the controls on the form and if they are disabled then the Ajax Combo box cannot load its post back data.

One work around would be to Response Redirect to another page after the invoice is posted. This way the controls would not try to load postback data again.

Removing the method to disable the controls also stopped the issue so replacing the combo box with a text box was a simple work around.

Just to re iterate its the control being disabled that has caused the problem when the control is trying to load the postback data.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:





An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:





[NullReferenceException: Object reference not set to an instance of an object.]
   AjaxControlToolkit.ComboBox.LoadPostData(String postDataKey, NameValueCollection postCollection) +106
   AjaxControlToolkit.ComboBox.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +36
   System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +693
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1743

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:

Syntax

DBCC SHRINKDATABASE
( database_name [ , target_percent ]
[ , { NOTRUNCATE | TRUNCATEONLY } ]
)

Arguments

database_name

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

target_percent

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

NOTRUNCATE

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.

TRUNCATEONLY

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.

Example


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.

aspx:

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:

Tuesday 5 January 2010

Grub Error 17

Yesterday i had a problem with my laptop caused by formatting a linux partition without first removing the bootloader. usually this is a simple fix and can be resolved by booting a windows xp cd, using the recovery console and doing a fixmbr.

This would have been great if i could get into the BIOS to change the boot order so i could set CD to boot before the hard disk. Unfortunately BIOS would not let me in as it was not accepting any keystrokes on boot. After exhausting almost every option and beginning to take the laptop apart to remove the CMOS battery in the hope the default setup has Cd boot before HDD i thought, i know, i reckon a Linux live Cd could sort this out. I had tried windows 7 and Windows xp CD's but neither would read the hard disk in the laptop

I had to remove the hard disk and put it in another laptop, then i could boot from a Live Cd and fix the MBR.

i used an ubuntu 8.04 cd in the end which had almost everything i needed. I had to install lilo to write the boot record as ms-sys was not accessible due to Microsoft making them pull it from the repository.

lilo can be installed with something like

sudo apt-get install lilo

after this is installed you need to find the partition with the mbr. This can be seen with the following command to list hard drive partitions

sudo fdisk -l

once you know the drive with the boot sector you can then do something like

sudo lilo -M /dev/sda mbr

This will overwrite the MBR. after this had run i put the hard disk back in the existing latop and it booted back into windows 7 with no problems.

thanks again Linux! so many times you have saved me!

Custom Globalization issues in deployment





I thought I should start my first post with a recent problem i have encountered.

I had a web application that used a Custom Culture to change the text of certain fields on the pages for a specific customer. For this I created a custom culture and created separate resource files for the pages.

This all worked fine on my development machine but when deployed to the production server the site would not run wit the following error...

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0101: The namespace 'Resources' already contains a definition for 'Global'


This was caused because there was no custom culture on the server so both resources were being compiled into the same class.

After a bit of searching I found Microsoft Locale Builder this would have been perfect but unfortunately it only works on windows vista!? This gave me the idea of creating my own.

Below is the code listing for the form, it can be used to create a custom culture based on a previous culture on the system. Custom Cultures can also be removed.