TableHeaderScope.Column and HTML standards compliance

Neil spotted this quirky behaviour in the TableHeaderCell control. Suppose you’ve got a Table web control that renders a <table> tag with a <th scope=”col”>, using code something like this:

Table table = new Table()
{
    Rows =
    {
        new TableRow()
        {
            Cells =
            {
                new TableHeaderCell()
                    { Scope = TableHeaderScope.Column, Text = "MyColumn" }
            }
        }
    }
};

Then the rendered HTML won’t be W3C compliant because a scope value of “column” isn’t valid:

<table border="0">
    <tr>
        <th scope="column"></th>
    </tr>
</table>

According to the W3C HTML and XHTML specs, the acceptable values for scope are [row|col|rowgroup|colgroup]. This is confirmed by the W3C Markup Validation Service, which reports the error:

W3 TH error

Stepping into the source code for the TableHeaderCell control with Reflector or the .NET Reference Source, you can see the root of the problem:

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
    TableHeaderScope scope = Scope;

    if (scope != TableHeaderScope.NotSet)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Scope,
            scope.ToString().ToLowerInvariant());
    }
}

When the control sets the scope attribute’s value, it uses a lowercase string representation of the TableHeaderScope.Column enumeration, i.e. “column”. An enum value of TableHeaderScope.Col would’ve been ok, but TableHeaderScope.Column is invalid.

Workarounds

Neil has a good workaround by adding the scope attribute manually instead of using the TableHeaderCell.Scope property:

TableHeaderCell th = new TableHeaderCell();
th.Attributes["scope"] = "col";

I thought I’d have a go at extending this fix into a simple control adatper that makes sure TableHeaderCell always renders itself correctly. It works by checking whether the Scope property is set to TableHeaderScope.Column, and if so manually adds the attribute scope=”col” instead. Here’s the code:

    public class TableHeaderCellAdapter : WebControlAdapter
    {
        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            TableHeaderCell th = (TableHeaderCell) this.Control;

            if (th.Scope == TableHeaderScope.Column)
            {
                th.Scope = TableHeaderScope.NotSet;
                th.Attributes["scope"] = "col";
            }

            base.RenderBeginTag(writer);
        }
    }

To associate the control adapter class with the TableHeaderCell control, add a browser definition file to the App_Browsers folder:

    <!-- file: ~/App_Browsers/ControlAdapters.browser -->
    <browsers>
        <browser refID="Default">
            <controlAdapters>
                <adapter
                  controlType="System.Web.UI.WebControls.TableHeaderCell"
                  adapterType="TableHeaderCellAdapter" />
            </controlAdapters>
        </browser>
    </browsers>

Configuring IIS7 for ASP.NET on Windows Vista

As part of Microsoft’s ongoing Trustworthy Computing initiative, Vista takes the secure by default principle more seriously than previous versions of Windows. Consequently, many features aren’t installed by default, one of which is IIS 7. So, to install IIS go to:

Control Panel – Programs – Turn Windows features on or off

Then find the Intenet Information Services section and add everything you need, including ASP.NET.

ASP.NET Windows Features

Installing ASP.NET Membership, Roles and Profiles support in SQL Server

Here are few quick tips if you’re using the ASP.NET Application Services with SQL Server, like Membership, Roles, Profiles, Personalization or Web Events.  These built-in “building-block” services have SQL provider implementations in .NET that let you use a SQL Server 2000 or 2005 database as their data store. These are the framework classes that implement the SQL Server providers:

ASP.NET SQL Server Registration Tool

Adding support for these providers to your database is easy, just run the ASP.NET SQL Server Registration tool from the command-line (aspnet_regsql.exe) to launch the ASP.NET SQL Server Setup Wizard.  This will guide you through the process with a simple GUI:

cd C:\Windows\Microsoft.NET\Framework\v2.0.50727 aspnet_regsql.exe

By default, the wizard installs all five services : Membership, Roles, Profiles, Personalization and Web Events. However, if you need support for only some, say just Membership and Roles, then aspnet_regsql can be run from the command-line with specific options, for example:

 aspnet_regsql -S MySqlServer -E -A mr -d MyAspDatabase

This installs the Membership and Roles services (-A mr) on server MySqlServer in database MyAspDatabase using current Windows credentials for authentication.  To see a full list of command-line options and switches, run the registration tool with the help flag:

 aspnet_regsql.exe /?

There’s also an option to generate just SQL scripts without executing them, and another to remove services from the database that aren’t used or needed anymore. (You can also manage SQL cache dependencies and session state using this tool.)

For more info about ASP.NET SQL providers:

New and upcoming books for ASP.NET developers

The are some great books in the pipeline for ASP.NET web developers.  Everything from Visual Studio 2010, ASP.NET 4.0, ASP.NET MVC, HTML 5, jQuery 1.4, and even iPhone development.  Not to mention second editions of classic titles like jQuery in Action and C# in Depth.

Got any more? Let me know in the comments.

O’Reilly

Web Design for Developers – A Programmer’s Guide to Design Tools and Techniques

Web Design for Developers will show you how to make your web-based application look professionally designed. We’ll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes–your application will really come alive. We’ll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

Effective UI – The Art of Building Great User Experience in Software

Effective UI provides a complete roadmap to building groundbreaking software centered on user experience (UX) quality, how to get management support, employing product management strategies proven to deliver greater success, and how to manage the design, engineering, staffing, and business considerations that must be centered on the user’s needs and working effectively in tandem all throughout the project. Effective UI is a guide for business and product, software professionals, and anyone else struggling to advance the cause of better UX and working to deliver on the promise of higher quality software.

Search Patterns
Search is among the most disruptive innovations of our time. It influences what we buy and where we go. It shapes how we learn and what we believe. This provocative and inspiring book explores design patterns that apply across the categories of web, ecommerce, enterprise, desktop, mobile, social, and realtime search and discovery. Using colorful illustrations and examples, the authors bring modern information retrieval to life, covering such diverse topics as relevance ranking, faceted navigation, multi-touch, and augmented reality. Search Patterns challenges us to invent the future of discovery while serving as a practical guide to help us make search better today.

97 Things Every Programmer Should Know – Collective Wisdom from the Experts
Get 97 short and extremely useful tips from some of the most experienced and respected practitioners in the industry, including Uncle Bob Martin, Scott Meyers, Dan North, Linda Rising, Udi Dahan, Neal Ford, and many more. They encourage you to stretch yourself by learning new languages, looking at problems in new ways, following specific practices, taking responsibility for your work, and becoming as good at the entire craft of programming as you possibly can.

High Performance JavaScript
Do you want to fix performance bottlenecks in your JavaScript code to help your website function better? With this book, you’ll examine the typical code problems that cause JavaScript to run slow. High Performance JavaScript teaches you how to identify inefficient structures and patterns, and replace them with better-performing alternatives.

Programming Microsoft® ASP.NET MVC
Author Dino Esposito leads you through the features, principles, and pillars of the ASP.NET MVC framework, demonstrating how and when to use this model to gain full control of HTML, simplify testing, and design better Web sites and experiences.

APress

Ultra-Fast ASP.NET: Building Ultra-Fast and Ultra-Scalable Websites Using ASP.NET and SQL Server
Ultra-Fast ASP.NET presents a practical approach to building fast and scalable web sites using ASP.NET and SQL Server. In addition to a wealth of tips, tricks and secrets, you’ll find advice and code examples for all tiers of your application, including the client, caching, IIS 7, ASP.NET, threads, session state, SQL Server, Analysis Services, infrastructure and operations. By applying the ultra-fast approach to your projects, you’ll squeeze every last ounce of performance out of your code and infrastructure—giving your site unrivaled speed.

Introducing .NET 4.0: with Visual Studio 2010
Microsoft is introducing a large number of changes to the way that the .NET Framework operates. Familiar technologies are being altered, best practices replaced, and developer methodologies adjusted. Many developers find it hard to keep up with the pace of change across .NET’s ever-widening array of technologies. You may know what’s happening in C#, but how about the Azure cloud? How is that going to affect your work? What are the limitations of the new pLINQ syntax? What you need is a roadmap. A guide to help you see the innovations that matter and to give you a head start on the opportunities available in the new framework.

Pro HTML5 Programming: Powerful APIs for Richer Internet Application Development
HTML5 is here, and with it, rich Internet applications (RIAs) take on a power, ease, scalability, and responsiveness that neither Ajax nor Comet could ever achieve. With this book, developers will learn how to use the latest cutting-edge web technology—available in the most recent versions of modern browsers—to build real-time web applications with unparalleled functionality, speed, and responsiveness.

Pro ASP.NET MVC V2 Framework
Author Steven Sanderson has seen the ASP.NET MVC Framework mature from the start, so his experience, combined with comprehensive coverage of all the new features, including those in the official MVC development toolkit, offers the clearest understanding of how this exciting new framework can improve your coding efficiency. With this book, you’ll gain invaluable up-to-date knowledge of security, deployment, and interoperability challenges.

iPhone and Mac OS X Development for Windows Programmers: C#, C++ and C Edition
When coming to the iPhone and Mac OS X from Windows, you need to learn a new set of coding techniques and tools. Taken blindly, this can come as a shock—a shock that this book will help you avoid. It does this by teaching you to use your existing Windows programming knowledge to ease you into programming for Apple technologies.

Manning

jQuery in Action, Second Edition
A really good web development framework anticipates your needs. jQuery does more—it practically reads your mind. Developers fall in love with this JavaScript library the moment they see 20 lines of code reduced to three. jQuery is concise and readable. Its unique “chaining” model lets you perform multiple operations on a page element in succession. And with version 1.4, there’s even more to love about jQuery, including new effects and events, usability improvements, and more testing options.

Brownfield Application Development in .NET

Brownfield Application Development in .Net shows you how to approach legacy applications with the state-of-the-art concepts, patterns, and tools you’ve learned to apply to new projects. Using an existing application as an example, this book guides you in applying the techniques and best practices you need to make it more maintainable and receptive to change.

Secrets of the JavaScript Ninja
In Secrets of the JavaScript Ninja, JavaScript expert John Resig reveals the inside know-how of the elite JavaScript programmers. Written to be accessible to JavaScript developers with intermediate-level skills, this book will give you the knowledge you need to create a cross-browser JavaScript library from the ground up.

Dependency Injection in .NET
Dependency Injection in .NET is a comprehensive guide than introduces DI and provides an in-depth look at applying DI practices to .NET apps. In it, you will also learn to integrate DI together with such technologies as Windows Communication Foundation, ASP.NET MVC, Windows Presentation Foundation and other core .NET components.

C# in Depth, Second Edition

C# in Depth, Second Edition concentrates on the high-value features that make C# such a powerful and flexible development tool. Rather than re-hashing the core of C# that’s essentially unchanged since it hit the scene nearly a decade ago, this book brings you up to speed with the features and practices that have changed with C# from version 2.0 onwards.

MS Press

Microsoft ASP.NET Internals
A comprehensive reference outlining the underpinnings of Microsoft’s Web application environment
Learn how to fully exploit the critical components and rich capabilities of the ASP.NET framework, without scouring blogs for information or enduring hours of trial and error. This comprehensive book offers developers the insights they need—including established architecture patterns—to create better, stronger, faster Web sites and Web-based applications Web services. The book offers readers the guidance they need to take advantage of ASP.NET right away, and outlines the evolution and future of the technology.

Addison Wesley

LINQ to Objects Using C# 4.0: Using and Extending LINQ to Objects and Parallel LINQ (PLINQ)
Using LINQ to Objects, .NET developers can write queries over object collections with the same deep functionality that was once available only with SQL and relational databases. Now, for the first time, developers have a comprehensive and authoritative guide to applying LINQ to Objects in real-world software. Microsoft MVP Troy Magennis introduces state-of-the-art techniques for working with objects more elegantly and efficiently–and writing code that is exceptionally powerful, robust, and flexible.

Updates from comments:

.NET Performance Testing and Optimization (Part 1)

Paul Glavich and Chris Farrell’s upcoming book on .NET performance testing and optimization will be out in a few weeks, and part one is already as a free e-book.  They explain why performance testing is a good idea, and walk you through everything you need to know about setting up a test environment. An essential handbook for programmers looking to set up a .NET testing environment.

Horse sense: Agile versus Waterfall

Check out Stuart Cam’s excellent comparison of agile versus waterfall development models, illustrated as drawing a horse:

Horse sketch development models

That freakish Frankenstein waterfall horse is going to give me nightmares.

New VS 2010 and .NET 4.0 training on msdev

Microsoft’s just added eight hours of .NET 4.0 and Visual Studio 2010 sessions to its training site msdev.com with New Features in .NET Framework 4 and Visual Studio 2010, Beta 2.

Subjects covered are:

  • Parallelism
  • ADO.NET Data Services
  • F#
  • Office Programmability
  • ASP.NET AJAX
  • ASP.NET MVC
  • ASP.NET WebForms
  • Managed Languages

And there’s even more ASP.NET 4.0 training in the ASP.NET 4.0 Quick Hit Videos series.  A great introduction to loads of new features in bite-size pieces:

  • Chart Control
  • Dynamic Metadata
  • Permanent Redirect
  • Imperative WebForms Routing
  • Declarative WebForms Routing
  • Outbound WebForms Routing
  • Auto Start
  • Clean Web.Config Files
  • Predictable Client IDs
  • Selective View State
  • The HtmlEncoder Utility Method
  • New Rendering Option for Check Box Lists and Radio Button Lists
  • Persistent GridView Row Selection
  • Table Free Templated Controls
  • Easy State Compression
  • Tableless Menu Control
  • Imperative JavaScript Syntax for Microsoft Client Side Controls
  • The ScriptLoader
  • jQuery Syntax for Microsoft Ajax
  • AJAX Data Templates
  • Hidden Field Divs
  • Disabled Control Styling

.NET Framework 3.5 Reference Poster

This slick .NET 3.5 reference poster is available as a free download from MSDN. It’s got the most commonly used types and namespaces in the framework.  A great quick reference for any .NET developer’s office wall:

.NET 3.5 Poster

As it’s getting progressively harder to keep up with .NET’s continutally expanding scope, this poster is a handy reminder of what’s included.  It shows which classes were added in .NET 3.0, and which in .NET 3.5.

There’s a broad cross-section across all areas of the .NET Framework:

  • ASP.NET
  • WinForms and WPF
  • Communications and Workflow
  • Data, XML and LINQ
  • Fundamentals

Printing

The hi-res version is Microsoft XPS format, so if you’re not using Vista or Office 2007 then you might want the Microsoft XPS Viewer. Also, for ‘easy printing’, there’s a 16 page 4×4 version, but remember: ’some assembly is required if you choose this print method’, so remember to ask an adult for help with the scissors.

My local print shop printed and laminated the PDF version onto A1, which is easily hi-res enough and looks great.

Other .NET Reference Posters

There are a few other reference posters on MSDN, in particular I like the Silverlight Developer Reference, and keyboard shortcuts for Visual Studio 2008, available for both C# and Visual Basic.

(Thanks to Chris Bowen for the tip off.)

SQL Server 2008 will have IntelliSense

With so many exciting new features in Visual Studio 2008 to explore, I haven’t had much time to look at the preview releases of SQL Server 2008 (aka Katmai). The last I heard, there wouldn’t be that many new goodies for developers, mainly features for DBAs and BI analysts with a few performance optimisations thrown in.

The last upgrade, SQL Server 2000 to 2005, was a huge leap forward for developers and added significant advances like CLR integration, SQL Server Management Objects (SMO), Integration Services (SSIS) and a native XML data type; as well as T-SQL enhancements like Common Table Expressions (CTEs), structured error handling with try/catch, pivot, apply, top(n) and row_number.

I was surprised to see how much new stuff is packed into the latest SQL Server 2008 CTP release, even, finally, IntelliSense for Management Studio, which was much anticipated but conspicuously absent from SQL 2005:

SQL Server 2008 IntelliSense

Also notice the new collapsible code regions, just like you get in Visual Studio. Editing T-SQL has never been such fun! Although, you have to feel a bit sorry for RedGate, whose SQL Prompt plug-in has been filling the auto-completion gap for the last few years.

MCPD Web Developer exam 70-547 hints and tips

Further to my notes on studying toward MCPD certification, I’ve now taken the exam and am pleased to say I passed with a good score. The exam 70-547 is about designing and developing web applications using ASP.NET 2.0. It focuses on the full software development lifecycle and covers planning, architecture, design, testing and deployment, where as the prerequisite MCTS exams cover more of the actual coding and implementation details.

My main study guide was the official Microsoft Press book, MCPD Self-Paced Training Kit (Exam 70-547). It covered the syllabus thoroughly enough to get me through the exam, but like most similar books wasn’t much fun to read. It’s over 700 pages, and after the first few hundred the content starts to get fairly monotonous. It’s also difficult to skim because you might miss something essential for the exam. If you’re an experienced developer then you might not learn much, but if you’re new to coding then you should find plenty of useful information about broader areas of software development, such as architecture, data modelling and unit testing.

A lot of the testing chapters read like an advert for Visual Studio Team System (VSTS) and aggressively promote its unit testing and web load testing capabilities. There’s nothing wrong with that, it’s a Microsoft exam after all, but you will need Visual Studio Team Test if you want to work through the practical exercises (an evaluation version is available). The unit testing in VSTS is impressive, but doesn’t seem to offer much over the freely available NUnit. The web load testing is very good, and a good alternative to JMeter.

My best advice for the book is to read it in short frequent bursts. Try to read a little bit every day and you’ll easily work through it, so don’t be intimidated. If you find yourself slacking, just register for the exam and you’ll soon become motivated. And remember to use the 15% exam discount voucher that comes with the book.

My other study resource, which I also used for the MCTS exams, was the practice test from MeasureUp. The questions are very relevant to the actual exam, and helped highlight areas to focus my study. The most useful feature is that every answer has an explanation, so if you get one wrong you can see why and hopefully fill a gap in your knowledge.

The exam isn’t easy, but many questions appeal to common sense as well as technical expertise, and there aren’t any trick questions. There’s more than enough time to work through the 40 multiple-choice questions, so be careful to read each one closely and don’t panic. Some questions ask you to pick more than one answer so follow the instructions carefully. The passing score is 700 out of 1000.

According to Microsoft’s statistics there are only 2,147 MCPD web developers worldwide, so if you want to improve your skills, stand out in the job market, or negotiate a promotion then certification is a great career move.

My next goal is MCTS certification for SQL Server and exam 70–431. Then it shouldn’t be too long before the next generation of .NET 3.5 MCTS are available.

Finding orphaned stored procedures and user-defined functions in SQL Server

I’m currently working on a group of ASP.NET 2.0 websites deployed across about thirty countries. The local flagship site runs on an upgraded version of the original code, and I’m now in the process of bringing all the other sites onto the new improved version.

Over time, new features have been introduced to the site, and old ones removed. Consequently the SQL Server database now contains many redundant tables that aren’t used. So, before cascading out the current schema to the other countries, it’s time for a clean up.

I managed to identify about 60 tables that aren’t used by the application and can safely can be dropped or archived. However, I’m now left with hundreds of stored procedures (SPs) and user-defined functions (UDFs) that were associated with these tables, which can also be removed.

The problem was how to find these orphaned objects. My first approach was a small .NET console application which uses SQL Server Management Objects (SMO). It loops through all SPs and UDFs and finds any that have no dependencies.


public List<string> FindOrphans()
{
   Server server = new Server(".");
   Database db = server.Databases["MyDatabase"];
   List<string> orphans = new List<string>();

   // get list of SPs
   UrnCollection urns = new UrnCollection();
   foreach (StoredProcedure sp in db.StoredProcedures)
   {
      // exclude these objects
      if (sp.IsSystemObject) continue;
      if (sp.Name.StartsWith("aspnet_")) continue;
      urns.Add(sp.Urn);
   }

   // get dependencies
   DependencyWalker dw = new DependencyWalker(server);
   DependencyTree tree = dw.DiscoverDependencies(urns, true);

   // find all objects without any dependencies
   DependencyTreeNode node = tree.FirstChild;
   do {
      if (!node.HasChildNodes)
      {
         string name = new Urn(node.Urn).GetAttribute("Name");
         orphans.Add(name);
      }
      node = node.NextSibling;
   } while (node != null);

   return orphans;
}

This works fine, and helped satisfy my current obsession with SMO. But it’s a bit awkward, and not easily portable or modifiable, to have this pure database operation wrapped up in an executable. So I looked into doing the same thing with just a TSQL query.

-- Find all SPs and UDFs have no dependencies
select
    object_name(obj.[object_id]) as [orphaned_object_name],
    obj.type_desc as [object_type],
    'DROP ' +
    case obj.type_desc
        when 'SQL_STORED_PROCEDURE' then 'PROCEDURE'
        else 'FUNCTION'
    end
    + ' [' + object_name(obj.[object_id]) + ']'
from
    sys.objects obj
    left join (select distinct [object_id] from sys.sql_dependencies) dep
        on obj.object_id = dep.object_id
where
    type_desc in
        ('SQL_STORED_PROCEDURE','SQL_SCALAR_FUNCTION','SQL_TABLE_VALUED_FUNCTION')
    and object_name(obj.[object_id]) not like 'aspnet_%'
    and dep.object_id is null
order by
    obj.type_desc, object_name(obj.[object_id])

The query works by checking for dependencies in the catalog view sys.sql_dependencies. This, I think, is a neater solution. I also included an auto-generated column that writes the SQL drop the SP or UDF, which I copied and executed.

Now, if only I could find a quick way to check for dependencies between my application’s data access layer and the database…

kick it on DotNetKicks.com