Visual Studio Extension: Browse Current Project

March 30, 2011

I’ve just published my first Visual Studio Extension. It is a very simple one-command extension that allows you to view the contents of the folder that contains the current project in the Visual Studio Browser. At the moment, only Visual Studio 2010 is supported.

Instructions for Use

To view open the browser on the project’s folder, just click on the button in the Solution Explorer’s toolbar. The button is only visible when you have a solution loaded in Visual Studio.

The contents of the folder that contains the current project will be displayed in Visual Studio. That’s it!


SQL Server: Drop all Foreign Keys that Reference a Table

March 17, 2011

This post has moved to Yagni.net.

 


BBC Micro High Scores!

May 11, 2010

Pengo 46330

Labyrinth 63870

Monsters 10620

Snapper 41820

Air Lift 70460

Zalaga 59490

Pole Position 56600

Nemesis 16290

Space Eagles 11880

Starship Command 889

Chucky Egg 235330

Snake (fast) 1016

Planetoid 115400

Mr EE 103100


Preventing Users Navigating Away from an AJAX.Net Page without Saving their Changes

December 11, 2009

On ASP.Net / AJAX recent project I was asked to ensure that users cannot navigate away from a web form if they have made changes that have not yet been submitted.

Requirements

The requirements were:

  • Enable users to submit the form by clicking the “Save” or “Cancel” buttons.
  • When user navigates away using any other method, show a dialog that states that changes will be lost if the user continues. These methods to include:
    • Any other controls on the page
    • Back button
    • Refresh button
    • Typing another address in the address bar
    • Picking another page from history
    • Closing the web browser
    • Anything else
  • Only show the dialog if the form is “dirty” (ie. there are unsaved changes”
  • Work with IE6 (I know, I know).
  • The solution should work client-side, so must be written in JavaScript.

Note that his solution has not been tested with other browsers.

Preventing Users Navigating

When a user attempts to navigate using any of the methods above, the window.onbeforeunload event will fire. We can use this event to pop-up a dialog that warns the user that she has not submitted her changes, and asks her to confirm that she wants to continue.

Checking for Dirt

In order to check for dirtiness, we must compare the original value of all the controls on the page with their value when they are submitted. A number of sources suggest that the browser maintains the default value for controls, and that we can compare the current value with that default value in the onbeforeunload event handler. However, there are several issues with this approach:

  • The default value is generally reset following an AJAX postback. This means that we must preserve the control’s original values of the controls when they first load, so gain nothing from using the browser’s method for tracking value changes.
  • In IE6, the defaultSelected property of HTML OPTION controls is, by default, set to true for all items in a drop-down list. The ASP.Net list control appears to set this value explicitly, I didn’t want to have to do so on the equivalent HTML control. As a result of the default, however, it is often impossible to determine if the user has unselected an item or if they have simply not selected the item. As a result it is best to track changes to the selection of options ourselves, rather than using the browser’s faulty tracking mechanism.

Allowing Certain Controls to Submit without Warning

It is important that some controls are able to submit the form without the using being warned that changes have been made. Typically, these controls include a button that allows the user to save her changes, and possibly one to exit without saving.

In order to implement these, we set a flag that indicates that a dirtiness check is required, then use the client-side click event of the buttons to reset the flag so that the check isn’t performed when these buttons are clicked.

OnBeforeUnload Sometimes Fires Twice

There is a bug in IE6 that means that, under some circumstances, the onbeforeunload event fires twice. We don’t want the confirm dialog to be displayed twice when the user attempts to leave a page, so we need to take measures. It is easy to set a flag the first time the onbeforeunload event fires that indicates that we can ignore it the second time. The slight complication is that the user may click “cancel” to prevent the navigation when the dialog first appears. If that happens, we don’t want to suppress the confirmation dialog for subsequent attempts to navigate away from the page. The solution is to start a timer that will reset the flag if the user remains on the page, thus allowing the dialog to appear when the user attempts to navigate again.

The Code

Here’s the JavaScript that I use:


<script type="text/javascript">
<!--

// Function to maintain original default states for all fields.
//  In order to test for dirtiness, we will be checking if the default
//  value for each control matches its current value. However, this
//  default is not normally preserved across partial postbacks. We need to
//  preserve these values ourselves.
function keepDefaults(form) {

// Get a reference to the form (in ASP.Net there should only be the one).
var form = document.forms[0];

// If no original values are yet preserved...
if (typeof (document.originalValues) == "undefined") {

// Create somewhere to store the values.
document.originalValues = new Array();

}

// For each of the fields on the page...
for (var i = 0; i < form.elements.length; i++) {

// Get a ref to the field.
var field = form.elements[i];

// Depending on the type of the field...
switch (field.type) {

// For simple value elements...
case "text":
case "file":
case "password":
case "textarea":

// If we don't yet know the original value...
if (typeof (document.originalValues[field.id]) == "undefined") {

// Save it for later.
document.originalValues[field.id] = field.value;

}
break;

// For checkable elements...
case "checkbox":
case "radio":

// If we don't yet know the original check state...
if (typeof (document.originalValues[field.id]) == "undefined") {

// Save it for later.
document.originalValues[field.id] = field.checked;

}
break;

// For selection elements...
case "select-multiple":
case "select-one":

// The form is dirty if the selection has changed.

// For each of the options...
var options = field.options;
for (var j = 0; j < options.length; j++) {

var optId = field.id + "_" + j;

// If we don't yet know the original selection state...
if (typeof (document.originalValues[optId]) == "undefined") {

// Save it for later.
document.originalValues[optId] = options[j].selected;

}
}
break;
}

}
}

// Call function to preserve defaults every time the page is loaded (or is
// posted back).
Sys.Application.add_load(keepDefaults);

// Assume that a check for dirtiness is required.
//  If this value is still true, we will check for dirtiness when the page
//  unloads.
var dirtyCheckNeeded = true;

// Function to flag that a check for dirtiness is not required.
//  Called by Save and Cancel buttons to indicate that a dirty check is
//  not actually required.
function ignoreDirty() {
dirtyCheckNeeded = false;
}

// Function to check if the page is dirty.
//  The function compares the default value for the control (the one it
//  was given when the page loaded) with its current value.
function isDirty(form) {

// For each of the fields on the page...
for (var i = 0; i < form.elements.length; i++) {
var field = form.elements[i];

// Depending on the type of the field...
switch (field.type) {

// For simple value elements...
case "text":
case "file":
case "password":
case "textarea":

// The form is dirty if the value has changed.
if (field.value != document.originalValues[field.id]) {
// Uncomment the next line for debugging.
//alert(field.type + ' ' + field.id + ' ' + field.value + ' ' + document.originalValues[field.id]);
return true;
}
break;

// For checkable elements...
case "checkbox":
case "radio":

// The form is dirty if the check has changed.
if (field.checked != document.originalValues[field.id]) {
// Uncomment the next line for debugging.
//alert(field.type + ' ' + field.id + ' ' + field.checked + ' ' + document.originalValues[field.id]);
return true;
}
break;

// For selection elements...
case "select-multiple":
case "select-one":

// The form is dirty if the selection has changed.
var options = field.options;
for (var j = 0; j < options.length; j++) {
var optId = field.id + "_" + j;
if (options[j].selected != document.originalValues[optId]) {

// Uncomment the next line for debugging.
//alert(field.type + ' ' + field.id + ' ' + options[j].text + ' ' + options[j].selected + ' ' + document.originalValues[optId]);
return true;
}
}
break;
}
}

// The form is not dirty.
return false;
}

// Clicking on some controls in (at least) IE6 caused the onbeforeunload
// to fire *twice*. We use this flag to check for this condition.
var onBeforeUnloadFired = false;

// Function to reset the above flag.
function resetOnBeforeUnloadFired() {
onBeforeUnloadFired = false;
}

// Handle the beforeunload event of the page.
//  This will be called when the user navigates away from the page using
//  controls on the page or browser navigation (back, refresh, history,
//  close etc.). It is not called for partial post-backs.
function doBeforeUnload() {

// If this function has not been run before...
if (!onBeforeUnloadFired) {

// Prevent this function from being run twice in succession.
onBeforeUnloadFired = true;

// If the dirty check is required...
if (dirtyCheckNeeded) {

// If the form is dirty...
if (isDirty(document.forms[0])) {

// Ask the user if she is sure she wants to continue.
event.returnValue = "If you continue you will lose any changes that you have made to this record.";

}
}
}

// If the user clicks cancel, allow the onbeforeunload function to run again.
window.setTimeout("resetOnBeforeUnloadFired()", 1000);
}

// Hook the beforeunload event of the page.
//  Call the dirty check when the page unloads.
if (window.body) {
// IE
window.body.onbeforeunload = doBeforeUnload;
}
else
// FX
window.onbeforeunload = doBeforeUnload;

// -->
</script>

The source code for my Save and Cancel buttons looks like this:


<asp:Button ID="btnEdit" runat="server" Text="Save" OnClick="btnEdit_Click" OnClientClick="ignoreDirty();" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="btn btnCancel" OnClick="btnCancel_Click" OnClientClick="ignoreDirty();" CausesValidation="false" />

References

I looked at a range of solutions before developing the one I present here, including:


A Good IT Solution is Like a Good Theory

December 7, 2009

What makes a good IT solution?

Theory = “A coherent group of general propositions used as principles of explanation for a class of phenomena.” Random House Dictionary

Consider the analogy, Solution = Theory.

T: Explains a whole class of phenomena.

S: Solves a whole class of problems.

T: Predicts results that have not yet been discovered.

S: Highlights and solves problems that have not yet been defined.

T: Does not conflict with existing results.

S: Does not break existing solutions.

T: Parsimony: Everything else being equal, the simplest theory is to be preferred.

S: As simple as possible, but no simpler.

T: No one theory explains everything.

S: No one solution provides a universal panacea.

T: Models changing relationships between observed data over time

S: Models changing relationships between input data over time

References


SQL Server: Show all the FOREIGN KEYS that reference a TABLE

November 5, 2009

For SQL 2005+


DECLARE
     @tableName nvarchar(MAX)
SET
    @tableName = 'tableName' -- (1)

SELECT DISTINCT
     ccu.table_name,
     ccu.constraint_name
FROM
     INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN
     INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
          ON rc.unique_constraint_name = tc.constraint_name
INNER JOIN
     INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
          ON ccu.constraint_name = rc.constraint_name
WHERE
     tc.table_name = @tableName

Notes:

  • Change line (1) to specify the table name.

 


SQL Server: Show all the PRIMARY KEYS for a TABLE

November 5, 2009

For SQL Server 2005 + 


DECLARE

     @tableName nvarchar(MAX)
SET

     @tableName = 'tableName' -- (1)


SELECT

      tc.TABLE_NAME,

      tc.CONSTRAINT_NAME,

      kcu.COLUMN_NAME,

      kcu.ORDINAL_POSITION

FROM

      INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc

INNER JOIN

      INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu

      ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME

WHERE

      tc.CONSTRAINT_TYPE = 'PRIMARY KEY'

      AND

      tc.TABLE_NAME = @tableName OR @tableName IS NULL

ORDER BY

      tc.TABLE_NAME,

      kcu.ORDINAL_POSITION

Notes:

  • Change line (1) to specify the table name. Removing this line will list PRIMARY KEYS for all TABLES.

VB.Net: Determine if an Object or Class Implements and Interface

June 29, 2009

The Problem

You want to know if a class called MyClass implements interface IMyInterface, or the object myObject inplements interface IMyInterface.

Unfortunately, you can’t do:

If MyClass Is IMyInterface Then

The Solution

It isn’t exactly intuitive, but for a class you can do this:

If GetType(IMyInterface).IsAssignableFrom(MyClass) Then
    ' Do something interesting, e.g.
    CType(MyObject, IMyInterface).MethodOnIMyInterface()
End If

Or for an object:

 If GetType(IMyInterface).IsAssignableFrom(MyObject.GetType) Then
    ' Do something interesting, e.g.
    CType(MyObject, IMyInterface).MethodOnIMyInterface()
End If

UltraWebTab: Switch to Tab on Validation Error

June 24, 2009

The Problem


I have an ASP.Net form that contains a set of tabs hosted inside an Infragistics UltraWebTab control. Each tab contains a number of fields that have validation. Sometimes, when the user submits the form, a validation error occurs. When this happens, I want the user to be able to correct the error with a minimum of fuss – I want the tab containing the error to be automatically selected, and the control with the error to be given the focus.

The Solution

The following code causes the first tab that contains an error to be selected when the form submits:

// These scripts are responsible for handling validation error messages in the tabs.
// If there are any errors on the page, they switch to the tab containing the first
// error and ensure that the control with the error is focused.

 function preValidate() {
  // For each of the validators on the page...
  var i;
  for (i = 0; i < Page_Validators.length; i++) {
   // Perform the validation.
   ValidatorValidate(Page_Validators&#91;i&#93;);

   // If invalid data is found...
   if (!Page_Validators&#91;i&#93;.isvalid) {

    // Get the control that failed validation.
    var badControl = document.getElementById(Page_Validators&#91;i&#93;.controltovalidate);

    // Focus it.
    focusInvalid(badControl);

    // Stop processing validation.
    return;

   }
  } // Next validator.

  // The page is valid.

 }

 function focusInvalid(badControl) {

  // Get the index of the tab that contains this control.
  var tabIndexToFocus = getContainingTabIndex(badControl);
  if (tabIndexToFocus > -1) {
   // Switch to the tab that contains the validation error.
   var ultraTab = igtab_getTabById('<%=UltraWebTab1.ClientID %>');
   ultraTab.setSelectedIndex(tabIndexToFocus);

   // Focus the control and exit.
   badControl.focus();
  }
 }

 function getContainingTabIndex(targetControl) {

  var ultraTab = igtab_getTabById('<%=UltraWebTab1.ClientID %>');

  // Get a reference to the tabs.
  var tabs = ultraTab.Tabs;

  // Start with the control that failed to validate.
  var testControl = targetControl;

  // While we have not got to the top of the control heirarchy...
  while (targetControl != null) {
   // Walk up the DOM to seek out the tab panel that contains the target control.
   testControl = testControl.parentNode;

   // For each of the tab panels...
   var tabIndex;
   for (tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
    if (tabs&#91;tabIndex&#93;.elemDiv == testControl) {
     return tabIndex;
    }
   } // Next index.

  } // Wend.

  // The control is not contained in a tab panel.
  return -1;
 }        
&#91;/sourcecode&#93;

To run the script, attatch it to the OnClientClick event of the button that submits the form:

&#91;sourcecode language='html'&#93;<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnEdit_Click" OnClientClick="preValidate();" />

Note that validation of each control will happen twice if the form is valid; once for the code above and then once because that’s how ASP.Net works. This doesn’t normally matter with the standard ASP.Net controls, but it might be an issue with badly-written custom validation controls. You have been warned!


MIDP 1.0 fillTriangle Method

June 1, 2009

One of the (many) limitations of MIDP 1.0 is that it lacks the fillTriangle that is present in MIDP 2.0. Below is a replacement method that works on MIDP 1.0 devices. It is based on notes published by Michal Wronski.

Michal’s algorithm depends on floating-point math – also unavailable in MIDP 1.0. With a little refactoring I was able to create an integer-only approximation. Sadly, rounding errors mean that my routine isn’t pixel perfect. An alternative that would give more accurate results would be to use some kind of fixed-point library. Despite its shortcomings, however, my method is good enough for many practical purposes (including my own).

protected void drawHorizontalLine(int x1, int x2, int y)
{
graphics.drawLine(x1, y, x2, y);
}

protected void fillTriangle(int ax, int ay, int bx, int by, int cx, int cy)
{
// http://www.geocities.com/wronski12/3d_tutor/tri_fillers.html

// Sort the points so that ay <= by <= cy int temp; if (ay > by)
{
temp = ax;
ax = bx;
bx = temp;
temp = ay;
ay = by;
by = temp;
}
if (by > cy)
{
temp = bx;
bx = cx;
cx = temp;
temp = by;
by = cy;
cy = temp;
}
if (ay > by)
{
temp = ax;
ax = bx;
bx = temp;
temp = ay;
ay = by;
by = temp;
}

// Calc the deltas for each edge.
int ab_num;
int ab_den;
if (by – ay > 0)
{
ab_num = (bx – ax);
ab_den = (by – ay);
}
else
{
ab_num = (bx – ax);
ab_den = 1;
}

int ac_num;
int ac_den;
if (cy – ay > 0)
{
ac_num = (cx – ax);
ac_den = (cy – ay);
}
else
{
ac_num = 0;
ac_den = 1;
}

int bc_num;
int bc_den;
if (cy – by > 0)
{
bc_num = (cx – bx);
bc_den = (cy – by);
}
else
{
bc_num = 0;
bc_den = 1;
}

// The start and end of each line.
int sx;
int ex;

// The heights of the two components of the triangle.
int h1 = by – ay;
int h2 = cy – by;

// If a is to the left of b…
if (ax < bx) { // For each row of the top component... for (int y = 0; y < h1; y++) { sx = ax + ac_num * y / ac_den; ex = ax + ab_num * y / ab_den; drawHorizontalLine(sx, ex, ay + y); } // For each row of the bottom component... for (int y = 0; y < h2; y++) { int y2 = h1 + y; sx = ax + ac_num * y2 / ac_den; ex = bx + bc_num * y / bc_den; drawHorizontalLine(sx, ex, by + y); } } else { // For each row of the bottom component... for (int y = 0; y < h1; y++) { sx = ax + ab_num * y / ab_den; ex = ax + ac_num * y / ac_den; drawHorizontalLine(sx, ex, ay + y); } // For each row of the bottom component... for (int y = 0; y < h2; y++) { int y2 = h1 + y; sx = bx + bc_num * y / bc_den; ex = ax + ac_num * y2 / ac_den; drawHorizontalLine(sx, ex, by + y); } } } [/sourcecode] I release this code into the public domain. You can use it as you please.