ASP.Net Page_Load and Page_LoadComplete

February 25, 2009

My application dbscript has a simple page structure for most pages: Each page related to a single record has a menu control displaying the links and actions for the current record.

For example, the Project page proj_edit.aspx embeds a proj_menu.ascx control, which retrieves some data from the database in the control’s Page_Load event.

If you want to access this data from the embedding page through the ascx control, you cannot access it in the page’s Page_Load event, as the Page_Load of the control has not yet fired. There is another event, less known (at least I did not know it), called Page_LoadComplete, which fires after each embedded control’s Page_Load has been processed.

To implement a Page_LoadComplete event in a page, you need to set

AutoEventWireup="true"

in the @Page directive of the aspx file.

Then simply add the lines

protected void Page_LoadComplete(object sender, EventArgs e)
{
}

to your page class in the aspx.cs file.

I posted about the ASP.Net page life cycle about a year ago, but recently came across this post which has a more detailed diagram on the page and control life cycle.

Download it. Memorize it. Make it your desktop background! ;)


‘Backup failed for Server’ raised in SMO Backup

February 24, 2009

Since I developed the first version of my script automssqlbackup, I kept getting a ‘Backup failed for Server’ exception on one specific database. All other databases created their backups without error. The only visible difference between the failing database and all other is its size: some 40 GB which generate a differential backup of about 500MB per day.

Some research brought, among numerous questions in forums, this MS KB entry: FIX: You receive an error message when you try to create a differential database backup in SQL Server 2005. It provides a hotfix and indicates that the problem might be fixed in a post-SP1 service pack.

I applied SP3 to SQL Server 2005, and the script can now backup all databases on the server successfully.

automssqlbackup is available for download here.


automssqlbackup sends notifications as HTML

February 23, 2009

As automssqlbackup works fine on the machines that I installed it, there’s always opportunity to fine-tune. This time it’s the notification emails being sent.

I modified the PowerShell script to send HTML emails.

The first part of the email contains a table with the list of selected databases, the status (“OK” or message of an exception), the name of the generated file or archive, and the sizes of the .bak and .zip files.

The second part contains the script’s transcript (i.e. the original mail contents).

automssqlbackup is available for download here.


JavaScript pageLoad() for MasterPage and ContentPage

February 22, 2009

I was experimenting with the DragPanelExtender recently, and almost immediately ran into the question of how to position it upon opening.

There are a lot of articles to be found on this question, and they always deal with it by means of a JavaScript function called pageLoad(), which is called by the ASP.Net Ajax framework sometime while the page is loading.

Now my application already has a pageLoad() function in the MasterPage, so adding a function of the same name in the ContentPage just did not work, as it would not be called by the framework.

My knowledge of JavaScript is not too profound, and I found that a function definition in JavaScript is actually a property of the window object. The hint was a line in the MicrosoftAjax.js file:

if (window.pageLoad) window.pageLoad(this,a);

meaning: if there is a defined function named pageLoad(), it is a property of the window object, and the value of the property is not null (== not false), and I can call it.

The solution was clear to me soon: if I need a pageLoad() method in the ContentPage, I need to name it differently and check for its definition in the MasterPage.

The pageLoad() in the MasterPage looks like this:

function pageLoad(sender, args)
{
    ...
    // ... do my MasterPage JavaScript stuff...
    ...
    if (window.contentPageLoad) {
        window.contentPageLoad(sender, args);
    }
}

and the ContentPage has its equivalent contentPageLoad() function:

function contentPageLoad(sender, args)
{
    // do something
}

Managed Windows API library for .Net

February 18, 2009

I mentioned accessing native Win32 APIs by .Net some time ago.

Today I came across a Sourceforge project called Managed Windows API, which provides a set of class to access various aspects of Win32. A couple of tools illustrate the use of the library.

I like TreeSizeSharp, a re-implementation of the TreeSize tool I often use if I find there’s not enough free space on my disks (again).


Updating automssqlbackup

February 18, 2009

A little bit of fine-tuning automssqlbackup:

After changing the zip functionality to SharpZipLib I modified the mail notifications to include the backup schedule (daily, weekly, monthly) in the subject, and also added an indicator whether the databases were backup up successfully.

automssqlbackup 0.27 is available for download here.


Zipping Files with PowerShell

February 11, 2009

When I developed automssqlbackup, one of the key features was to optionally zip the generated database backup file.

I found solutions like this which essentially create an empty zip archive, then create a Shell.Application object to access this archive, and add files using the CopyHere or MoveHere methods of that object.

However, this approach has some severe drawbacks:

  • Zip file creation and access via Shell.Application can collide occasionally, so that the COM object cannot access the zip file
  • If such a collision occurs, the shell will display a message box which the user has to confirm/cancel, and the caller (the Powershell script, in this case) is not notified about the error situation
  • The MoveHere method does not seem to *move* in the sense that the original file is deleted after being added to the zip archive

(short rant: this is automation the Microsoft way. Provide a programming interface, and if things go wrong, display a message box. Provide functionality as asynchronous operation and not tell the caller when it’s finished. If this was *real* automation, the caller would receive an error notification and a success notification from the callee, and the caller would decide how to react)

Since these effects showed up soon after I tested my script on different machines, it was clear that zipping had to be solved more reliably: using SharpZipLib.

First SharpZipLib is loaded into the Powershell script:

[System.Reflection.Assembly]::LoadFrom("C:\path-to\ICSharpCode.SharpZipLib.dll")

I found there are 2 simple ways to create a zip archive and add a file to it:

Using ZipFile.Create() and *Update()

$zip = [ICSharpCode.SharpZipLib.Zip.ZipFile]::Create($zipname)
$zip.BeginUpdate()
$zip.Add($filename)
$zip.CommitUpdate()
$zip.Close()

Quick and easy, but with a drawback: the filenames are stored including their original path information.

Using FastZip.CreateZip()

$zip = New-Object ICSharpCode.SharpZipLib.Zip.FastZip
$zip.CreateZip($zipname, $filenamedir, $false, "\.bak$")

$filenamedir is the base directory of all files to be included in the zip, followed by the recursive flag.

The final parameter is a bit tricky: it is named FileFilter, but it is not a DOS-like filter (“*.*”), but rather a regular expression being evaluated on each file. In the example above, all files with extension .bak are selected.


Introducing automssqlbackup

February 10, 2009

There are many ways to automatically backup MS SQL Server databases even if you use an Express version and do not have SQL Agent installed.

I sketched one possible solution some time ago, calling T-SQL commands from a batch file which is executed as a Scheduled Task.

If you are running MySQL databases under Linux, you may be aware of a tool called automysqlbackup, which runs as a cron job, creates dumps of MySQL databases and rotates daily and weekly backups.

During the last couple of days I translated this script into PowerShell to backup MS SQL Server databases in a similar way.

automssqlbackup provides the following functionality:

  • Local or Remote backup
  • Rotation of daily and weekly backups
  • Optionally zipping backups
  • Logging
  • Notifications by e-mail

The first release of automssqlbackup is version 0.25 to reflect the current version 2.5 of automysqlbackup, which it is based on.

automssqlbackup is available for download here.


Generating HTML Documentation of MS SQL Database

February 3, 2009

In previous posts I described how to generate MediaWiki documentation of an MS SQL database schema and selected table data using dbscript.

The upcoming version 0.94 provides the functionality to create a single HTML file documenting the database schema of a selected MS SQL server database.

This page contains the documentation of the AdventureWorks database (MSSQL 2005) as it is generated by dbscript 0.94 using the “SingleHtml ProjectVersion” stylesheet which is included in the next release.

dbscript is available for download here.