Coding, content and startups

James Crowley

Archive for November, 2005

Why is the HtmlHead class sealed?

with 6 comments

ASP.NET 2.0 gives us a Page.Title property, which we can set in code, or in the Page directive. Great! Unfortunately, I had a requirement so that whilst I’d be setting a portion of the title from the page, the rest would be pre-defined (ideally within the master page that I use). Obviously you can’t fiddle the stuff in the server-side title tag, because the contents just gets overriden. So I thought I’d just extend the new System.Web.UI.HtmlControls.HtmlHead class and fiddle the Title property to add the required string to the end Except you can’t. Because its sealed. Why? And does anyone have another way to do this?

Written by james.crowley

November 22nd, 2005 at 11:27 pm

Posted in Uncategorized

Enhanced UK Developer Event Listings

with 2 comments

I've now got some enhanced UK developer event listings up on Developer Fusion – check it out at UK Developer Events. Suggestions for improvements are always welcome. Made a nice use of the Google Maps API to do this… (sorry Microsoft, but the MapPoint service is way out of my price league!). Next on the cards will be setting up an extended RSS format for events (there doesn't seem to be one at the moment?) so we can pull information directly from UK user groups such as VBUG.

Watch this space!

If you run a user group or other developer (or IT-pro) related events in the UK, drop me a line and we'll get information about your upcoming events listed, along with a page for your organisation.

Written by james.crowley

November 21st, 2005 at 7:34 pm

Posted in Uncategorized

Multiple CSS Classes

with 6 comments

I’m not sure I should really admit to not knowing this… but today was the first time I’d realised that the “class” attribute for elements within a HTML document can accept more than one class name. doh.

Update: Here’s a simple example. With the following in your style sheet…

.align-c { text-align: center; }
.font-b { font-weight: bold; }

You can have something like <div class=”align-c font-b”></div> to make the contents of the div tag bold and centre-aligned.

Written by james.crowley

November 17th, 2005 at 2:10 pm

Posted in Uncategorized

Caching Method Results in ASP.NET 2.0 using Delegates

with 5 comments

Hmm. Talk about over-engineering. I don’t think we really need generics at all, provided we’re happy with a cast outside the method instead of inside it.

public delegate object MethodExecution();
public static object GetCachedMethod(string key, DateTime absoluteExpiration, MethodExecution method)
{
    if (HttpContext.Current.Cache[key] == null)
        HttpContext.Current.Cache.Insert(key,
            method(),
            null, absoluteExpiration, Cache.NoSlidingExpiration);

    return HttpContext.Current.Cache[key];
   
}
...
return (DataSet)GetCachedMethod(key, DateTime.Now.AddDays(1),
                delegate() { return SomeMethodThatReturnsADataSet(myParam); });

Written by james.crowley

November 14th, 2005 at 10:17 pm

Posted in Uncategorized

Caching Method Results in ASP.NET 2.0 using Generics & Delegates

with 3 comments

Today I found myself coding a fairly familiar pattern – checking whether there was an entry in the ASP.NET cache with a particular key, if not, executing a method and adding the result to the cache, and either way, returning the result.

I wondered whether there was a “nice” way to do this using generics and anonymous delegates. This is what I came up with…

public delegate T MethodExecution<T>();
public static T GetCachedMethod<T>(string key, DateTime absoluteExpiration, MethodExecution<T> method)
{
    if (HttpContext.Current.Cache[key] == null)
        HttpContext.Current.Cache.Insert(key,
            method(),
            null, absoluteExpiration, Cache.NoSlidingExpiration);

    return (T)HttpContext.Current.Cache[key];
   
}

Now, to use this method,I could write the following to return a cached (by one day) result of the method SomeMethodThatReturnsADataSet.

return GetCachedMethod<DataSet>(key,DateTime.Now.AddDays(1),
                delegate() { return SomeMethodThatReturnsADataSet(myParam); });

I’m not sure whether this just makes things more obscure – any comments? :)

Written by james.crowley

November 14th, 2005 at 8:42 pm

Posted in Uncategorized

Re-setting Identity Column in SQL Server

with 5 comments

Discovered something new today – normally I’d just use the TRUNCATE TABLE command in order to reset an identity column in a table within SQL Server. However, SQL Server doesn’t let you do this if you’ve got foreign key constraints pointing at the table; so instead, I deleted all the rows using a standard DELETE statement, and then reset the identity with the following command:

DBCC CHECKIDENT (table_name, RESEED, new_reseed_value)

According to the docs, “The current identity value is set to the new_reseed_value. If no rows have been inserted to the table since it was created, the first row inserted after executing DBCC CHECKIDENT will use new_reseed_value as the identity. Otherwise, the next row inserted will use new_reseed_value + 1. If the value of new_reseed_value is less than the maximum value in the identity column, error message 2627 will be generated on subsequent references to the table.”

So, as we’ve already had rows in the table, I called the function with new_reseed_value  to zero – hence the next row inserted into the table gets an identity of “1″. Nice!

Written by james.crowley

November 10th, 2005 at 12:11 am

Posted in Uncategorized

AJAX in ASP.NET

with one comment

For those of you who missed my presentation on AJAX in ASP.NET at DDD II… or just want to see me mess up in my first demo again, you can now download a video of it from here. Enjoy! :-,,)

Written by james.crowley

November 2nd, 2005 at 10:41 am

Posted in Uncategorized