Sacha Tomey

Sacha Tomey's Blog

LoadControl Changes in .NET 2.0

In .Net 1.x you can dynamically load user controls using the Page.LoadControl member.  Providing you namespace your classes the same or qualify the classes correctly you can cast the return value of LoadControl to an object of your user control type.
 
MyUserControl myUserControl = (MyUserControl )LoadControl("MyUserControl.ascx");
MyFolderedUserControl myFolderedUserControl = (MyFolderedUserControl )LoadControl("Folder/MyUserControl.ascx");
 
Moving to .Net 2.0, where namespaces are no longer a key reference mechanism, an additional step is required.
 
In the partial code behind class, specify the loading and casting as you would in Net 1.x.
 
MyUserControl myUserControl = (MyUserControl)LoadControl("MyUserControl.ascx");
MyFolderedUserControl myFolderedUserControl = (MyFolderedUserControl)LoadControl("Folder/MyUserControl.ascx");
 
However, when you attempt to compile this code, the second line will fail, this is because the foldered user control is not part of the main assembly and the compiler knows nothing about it.  To fix this you need to add a reference attribute to the aspx file that will host the user control:
<%@ Reference Control="Folder/MyUserControl.ascx" %>

Invalidating ASP.NET OutputCache

You can invalidate ASP.NET output level cache using cache dependencies.
 
The ASP.NET webform should contain something like this:
 
<%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %>
 
If you need to invalidate the cache prior to the 60 seconds elapsing you can do this by setting up a cache dependency between the page and an item in the cache:
 

Response.AddCacheItemDependency("MyCachedData")

 

Now, when the "MyCachedData" cachekey is changed or invalidated the cached page is invalidated too.

Scripting SQL Server 2000 Jobs

SQL Server Agent Jobs can be scripted with the following SQL DMO VB Script:
 
Dim conServer
Dim fso
Dim iFile
Dim oJB
Dim strJob
Dim strFilename
Const ioModeAppend = 8
Set conServer = CreateObject("SQLDMO.SQLServer")
conServer.LoginSecure = True
conServer.Connect "SQL2K_Server"

strFilename = "c:\SQLJobs.sql"

For Each oJB In conServer.JobServer.Jobs
    strJob = strJob & "--------------------------------------------------" & vbCrLf
    strJob = strJob & "-- SCRIPTING JOB: " & oJB.Name & vbCrLf
    strJob = strJob & "--------------------------------------------------" & vbCrLf
    strJob = strJob & oJB.Script() & vbCrLf
Next
Set conServer = Nothing

Set fso = CreateObject("Scripting.FileSystemObject")
Set iFile = fso.CreateTextFile(strFilename, True)
iFile.Write (strJob)
iFile.Close
Set fso = Nothing

 

Enterprise Library Woes

When deploying the enterprise library to a production server the following errors (or similar) can be fixed by running the 'InstallUtil' utility against the enterprise library assemblies.
 

Failed to fire the WMI event 'DataConnectionOpenedEvent'. Exception: System.Exception: This schema for this assembly has not been registered with WMI.

at System.Management.Instrumentation.Instrumentation.Initialize(Assembly assembly)

at System.Management.Instrumentation.Instrumentation.GetInstrumentedAssembly(Assembly assembly)

at System.Management.Instrumentation.Instrumentation.GetFireFunction(Type type)

at System.Management.Instrumentation.BaseEvent.get_FireFunction()

at System.Management.Instrumentation.BaseEvent.Fire()

at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.FireWmiEventCore(BaseEvent baseEvent).

Lorem ipsum dolor sit amet

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In leo. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam a elit id turpis semper auctor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse bibendum luctus est. Duis pharetra mauris sed mauris. Aenean lacinia lectus iaculis ante. Vivamus pharetra ipsum sit amet nisi dapibus lacinia. Aliquam feugiat hendrerit lorem. Morbi vestibulum euismod augue. Proin tempor condimentum sem. Nulla facilisi. Proin vestibulum. Pellentesque tellus nisl, dignissim id, congue nec, egestas a, leo. Vivamus eget nunc sed nisi pretium nonummy. Vivamus dapibus lectus in eros.

Aliquam vulputate cursus libero. Vestibulum dictum, dui ut porta adipiscing, sem massa fringilla libero, at sagittis lorem sapien vel nisi. Nulla sit amet arcu sed dolor luctus placerat. Phasellus porta nisi quis eros. Nullam tristique mi feugiat arcu. Cras tempor luctus nibh. Phasellus vitae magna. Nulla porta quam eu risus. In bibendum. Nulla felis felis, scelerisque non, blandit ut, interdum in, pede. Sed ut tortor vitae justo pellentesque malesuada. Aenean sit amet dolor mattis lorem ultricies congue. In quis mauris. Ut blandit vulputate felis. Mauris dolor est, fringilla eu, laoreet sed, porta id, orci.

Sed a risus et nisl condimentum congue. Vestibulum tortor orci, auctor dapibus, fermentum vel, euismod ac, turpis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Fusce porta. In id justo. Duis fermentum pulvinar arcu. Aenean facilisis neque sit amet sem. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sagittis turpis sed lectus. Donec iaculis eleifend arcu. Donec odio lorem, tincidunt ut, sagittis quis, lacinia sed, orci. Proin a sem sit amet pede eleifend dapibus.

Sed enim purus, vehicula eu, viverra quis, hendrerit et, metus. Phasellus lacinia viverra urna. Pellentesque rutrum luctus lorem. In placerat nibh eget risus ornare cursus. Etiam venenatis malesuada erat. Integer a mauris vel risus pellentesque tincidunt. Aliquam erat volutpat. Proin et orci. Nunc dignissim lacinia nisi. Mauris sapien purus, commodo at, suscipit nec, venenatis nec, dolor. Aenean faucibus est vitae risus. Aliquam eleifend sem et neque.

Vestibulum condimentum nisl vel erat. Vestibulum varius enim quis magna. Praesent hendrerit consectetuer nulla. Vestibulum vitae metus eu libero venenatis accumsan. Nam scelerisque mauris ut eros. Vestibulum mattis. Sed nisi dui, tempor ut, semper ac, feugiat ut, orci. Vivamus porttitor enim quis ante. Nulla quis diam. Quisque ut turpis nec sem scelerisque ullamcorper. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

You really need more??

Recycle IIS6.0 application pools from code

Works with IIS6.0 Windows Server 2003 SP1...
 

public static void Recycle(string host, string poolname)

{

using

(DirectoryEntry apppool = new DirectoryEntry(string.Format("IIS://{0}/w3svc/apppools/{1}", host, poolname)))

{

apppool.Invoke("Recycle");

}

}

Microsoft Origami - I want one!

There's some serious buzz surrounding the Microsoft Origami Project and I eventually saw the video this morning after it was pulled from the Digital Agency that 'leaked' it.
 
I'm sure it's a conceptual device, 'accidentally leaked' to create some kind of buzz and excitement.  The continuity of the buttons give the game away for me but hey, if they make it, I'll buy it!  (Or rather five and sell the rest on ebay!)
 

Automated SQL Server 2000 Data Dictionary

 
The two stored procedures below will help with automating the generation and maintenance of a database data dictionary.
 
If you provide a description for each database field in a SQL Database, these stored procedures can be used to extract that description information together with some other useful column details.
 
The first stored proc simply details the column information for a single table:
 
CREATE PROCEDURE uxp_Table_Data_Dictionary
    @Table_Name varchar(50)
AS
-- Extract the table data dictionary
SELECT

    TABLE_NAME,
    ORDINAL_POSITION, 
    COLUMN_NAME, 
    CASE DATA_TYPE 
        WHEN 'varchar' THEN 'varchar('+CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(20))+')'
        ELSE DATA_TYPE
    END AS DATA_TYPE,
    ISNULL(COLUMN_DEFAULT, '') AS COLUMN_DEFAULT,
    IS_NULLABLE,
    value AS PROPERTY_VALUE

FROM
    INFORMATION_SCHEMA.COLUMNS AS info 
        JOIN ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', @Table_Name, 'column', default) AS extended ON info.COLUMN_NAME = extended.objname

WHERE

    TABLE_NAME = @Table_Name
The second stored procedure extends this a little and reports the column information for all user tables within the current database:
 
CREATE PROCEDURE uxp_System_Data_Dictionary
AS
    -- Create a cursor containing all user tables
    DECLARE @tableName varchar(40) 
    DECLARE table_cursor CURSOR FOR
    SELECT name FROM sysobjects WHERE type = 'U' AND name <> 'dtproperties'
    OPEN table_cursor
 
    -- Perform the first fetch.
    FETCH NEXT FROM table_cursor INTO @tableName
 
    -- Check @@FETCH_STATUS to see if there are any more rows to fetch.
    WHILE @@FETCH_STATUS = 0
    BEGIN
        -- Execute the table data dictionary generator
        EXEC uxp_Table_Data_Dictionary @tableName
 
        -- Fetch the next table
        FETCH NEXT FROM table_cursor INTO @tableName
    END
 
    -- Tidy Up
    CLOSE table_cursor
    DEALLOCATE table_cursor