navigation
 Monday, June 23, 2008
See a solution for a common problem encountered with the built-in ASP.NET membership controls
posted on June 23, 2008  #    by Adam Anderson  Comments [0]
 Tuesday, June 17, 2008

OK, so I'm a little late to the party. I've been using it for a few days now, and I'm very pleased. Browsing is even faster, and there are new features like the Quick Find address bar that performs full-text searches on every page in your browsing history. Give it a try at www.opera.com/download.

posted on June 17, 2008  #    by Adam Anderson  Comments [0]
 Monday, June 16, 2008

Lately I've needed to compare the schemas of two or more databases and highlight the differences. Based on internet research, I settled on two contenders: AdeptSQL Diff and Red Gate SQL Compare. Both are reasonably fast. AdeptSQL's UI looks very 16-bit and has limited reporting options. Red Gate's UI is very attractive and offers more reporting options. However, Red Gate's product currently has a bug that considers quoted and unquoted trigger identifiers to be different, even if the identifier itself is the same. This resulted in a large number of false differences, forcing me to go with AdeptSQL for now. If Red Gate can fix this problem, I would be inclined to switch to their product in the future.

posted on June 16, 2008  #    by Adam Anderson  Comments [0]
 Thursday, May 29, 2008
See how to rewrite T-SQL to reduce the use of cursors, which consume server resources and tend to be slower than set-based operations. Before and after example code included.
posted on May 29, 2008  #    by Adam Anderson  Comments [0]
 Friday, May 16, 2008
Learn how to speed up queries by breaking them down into smaller parts
posted on May 16, 2008  #    by Adam Anderson  Comments [0]
 Wednesday, May 07, 2008

This is actually right in the SQL Server Books Online, but not easily found. Given a temp table named #temp:

if ( object_id( 'tempdb..#temp' ) is null )
    -- table doesn't exist
else
    -- table exists
posted on May 7, 2008  #    by Adam Anderson  Comments [0]

Here is an interesting article on a way to turn many looping row-based operations into set-based ones: http://www.sqlservercentral.com/articles/TSQL/62867/

I think the idea is a clever one, and I love the "Row By Agonizing Row" (RBAR) acronym!

posted on May 7, 2008  #    by Adam Anderson  Comments [0]
 Tuesday, May 06, 2008

I'm a big fan of the Opera browser for its speed and excellent out-of-the-box functionality. One of the few things I have been willing to concede that it was lacking was a good interactive debugger like FireBug, until now. Opera Dragonfly has arrived!

posted on May 6, 2008  #    by Adam Anderson  Comments [0]
 Wednesday, April 30, 2008
When writing queries that group and aggregate, it is not uncommon for there to be many columns in the SELECT clause that are neither in the GROUP BY clause nor being aggregated. Learn two different ways to write such queries without compromising performance.
posted on April 30, 2008  #    by Adam Anderson  Comments [0]
 Friday, April 25, 2008

This article presents some interesting ideas on when and when not to use AJAX.

http://dev.opera.com/articles/view/stop-using-ajax/

posted on April 25, 2008  #    by Adam Anderson  Comments [0]
 Wednesday, April 23, 2008
SQL Server provides three different ways to retrieve identity values generated by the database. Here is a detailed description of how they differ.
posted on April 23, 2008  #    by Adam Anderson  Comments [0]
 Monday, April 07, 2008
Learn how and why to convert a scalar user-defined function to a table-valued one
posted on April 7, 2008  #    by Adam Anderson  Comments [0]
 Thursday, April 03, 2008
Prior to 2005, if you needed to access the value of a newly inserted IDENTITY column, you were bound to row-based operations so you could access each value using the SCOPE_IDENTITY() function. But now, there is another way...
posted on April 3, 2008  #    by Adam Anderson  Comments [0]
 Monday, March 10, 2008
ControlParameters are a really nice way to declaratively define the behavior of DataSources, but often you need values that aren't directly in controls. Here is how to bind a ControlParameter so that it reads any page property you define.
posted on March 10, 2008  #    by Adam Anderson  Comments [1]
 Monday, December 17, 2007
Three ways to access the query string as key-value pairs.
posted on December 17, 2007  #    by Adam Anderson  Comments [0]
 Tuesday, December 04, 2007
This technique is useful to show a page immediately upon request, then begin loading data after the page appears in the browser.
posted on December 4, 2007  #    by Adam Anderson  Comments [0]
 Tuesday, November 06, 2007
Read about the problems I encountered and the final solution I arrived at. Source code inside.
posted on November 6, 2007  #    by Adam Anderson  Comments [20]
 Thursday, October 04, 2007
Inside, I'll discuss a common SQL query pattern and a simple change that can increase query performance by a factor of two or more.
posted on October 4, 2007  #    by Adam Anderson  Comments [2]
 Monday, July 30, 2007
 | 
posted on July 30, 2007  #    by Adam Anderson  Comments [0]
 Wednesday, June 27, 2007

I stumbled upon this great series of articles that go into much more detail about functional programming in C#, the culmination of which is to write implementations of the "big three" higher-order functions: Map, Filter, and Reduce.

http://diditwith.net/PermaLink,guid,a1a76478-03d2-428f-9db6-9cf4e300ea0f.aspx

posted on June 27, 2007  #    by Adam Anderson  Comments [0]
 Friday, June 15, 2007

I just spent some quality time with Firefox and Firebug trying to figure out why my client-side validators weren't preventing a postback when invalid data was entered. The symptoms were puzzling: when I entered invalid data and then attempted to submit the form, the validators would appear briefly, but then the page would post anyway!

A little debugging revealed that the button I was clicking called the JavaScript function WebForm_DoPostBackWithOptions, which called Page_ClientValidate, which called ValidationSummaryOnSubmit. This function looped through a list of validation summaries named Page_ValidationSummaries and then performed operations on each list element, which naturally meant accessing its properties.

ValidationSummaryOnSubmit doesn't test each summary in the list to ensure that the summary is not null before attempting to access its properties, so if a null reference finds its way into this list, an error occurs, which causes the entire call chain to terminate abnormally, in this case resulting in a failure to prevent the form from posting.

A little more debugging followed, and I located where the Page_ValidationSummaries list is initialized, and found which reference was returning a null value. It was a validation summary in the footer of a DataGrid, and the DataGrid's ShowFooter property was false. This of course meant that all of the controls within the footer were never rendered to the client, but the code that generated the list of summaries was including the reference anyway.

The solution to the problem was clearly to find a way to get the generated script to exclude the summary when the footer wasn't visible. I tinkered with a few alternatives, ultimately settling on this one: set the summary's Enabled property to be databound to the expression:

Enabled='<%# DataBinder.Eval( Container.NamingContainer, "ShowFooter" ) %>'

Within templated controls such as a DataGrid, Container refers to the DataGridItem of the current row. DataGrids are naming containers for DataGridItems, so accessing Container.NamingContainer gives a reference to the DataGrid. Eval then uses reflection to find the ShowFooter property of the grid and assign it to the Enabled property of the contained ValidationSummary. My testing showed that with this expression, the summary no longer appeared in the Page_ValidationSummaries initialization list when the footer was hidden, and client-side validation prevented postbacks again. Problem solved!

posted on June 15, 2007  #    by Adam Anderson  Comments [0]
 Thursday, June 14, 2007

If you create a brand-new web application in ASP.NET 2.0 or later, client-side validation will automatically work in Firefox. However, if you have legacy applications originally written in ASP.NET 1.1 or earlier, client-side validation will not automatically work in Firefox. To enable this feature in legacy applications, simply open your web.config file and locate the xhtmlConformance element and change it to this:

<xhtmlConformance mode="Transitional"/>

posted on June 14, 2007  #    by Adam Anderson  Comments [0]
 Thursday, May 31, 2007

A higher-order function is defined as a function that returns a function as a result. Higher-order functions are common in the world of functional programming, but have only recently started to become more mainstream. As of C# 2.0, they are starting to become part of the language. A perfect example of their use would be with the generic List<T>.FindAll() method, which takes a Predicate<T> as an argument. A Predicate<T> is a delegate, which is a strongly typed method reference. One way to search for all the items in a list is to use anonymous delegates. For example, given a list of Falafel employees:

List<Falafel> falafels = new List<Falafel>();
falafels.Add( new Falafel( "Lino", "Tadros" ) );
falafels.Add( new Falafel( "John", "Waters" ) );
falafels.Add( new Falafel( "Noel", "Rice" ) );
falafels.Add( new Falafel( "Adam", "Anderson" ) );
falafels.Add( new Falafel( "Xavier", "Pacheco" ) );
falafels.Add( new Falafel( "Adam", "Markowitz" ) );
falafels.Add( new Falafel( "Mike", "Dugan" ) );
falafels.Add( new Falafel( "Bary", "Nusz" ) );
falafels.Add( new Falafel( "Rick", "Miller" ) );
falafels.Add( new Falafel( "Mike", "Saad" ) );
falafels.Add( new Falafel( "Eric", "Titolo" ) );

You could search for all employees whose name starts with "A" like this:

falafels.FindAll( delegate( Falafel item ) { return item.FirstName.StartsWith( "A" ); } )

Output:
Adam Anderson
Adam Markowitz

However, if you wanted to perform another search for all employees whose name starts with "M", you'd have to write an entirely different anonymous delegate. That's where higher-order functions can help. Let's create a higher-order function that will return a delegate that searches for Falafels whose first name starts with a string we pass as a parameter:

public static class Predicates
{
  public static Predicate<Falafel> FirstNameStartsWith( string s )
  {
    return delegate( Falafel item ) { return item.FirstName.StartsWith( s ); };
  }
}

Now we can search for employees whose name starts with any letter we want:

falafels.FindAll( Predicates.FirstNameStartsWith( "A" ) );
falafels.FindAll( Predicates.FirstNameStartsWith( "M" ) );

Output:
Adam Anderson
Adam Markowitz

Mike Dugan
Mike Saad

But the fun doesn't stop there! If you define a set of primitive Predicates, you can define more complex Predicates as a combination of the simpler ones. Here is how to define higher-order functions that take a list of Predicates and returns a Predicate that returns the result of evaluating each of them and combining the result with the logical AND or OR operator:

public static Predicate<Falafel> And( params Predicate<Falafel>[] predicates )
{
  return delegate( Falafel item )
  {
    foreach ( Predicate<Falafel> predicate in predicates )
      if ( !predicate( item ) )
        return false;
    return true;
  };
}

public static Predicate<Falafel> Or( params Predicate<Falafel>[] predicates )
{
  return delegate( Falafel item )
  {
    foreach ( Predicate<Falafel> predicate in predicates )
      if ( predicate( item ) )
        return true;
    return false;
  };
}

Using these, we can create a single Predicate that searches for all Falafels whose first name starts with "A" or "R" and whose last name starts with "M":

Predicate<Falafel> predicate = Predicates.Or( Predicates.FirstNameStartsWith( "A" ), Predicates.FirstNameStartsWith( "R" ) );
predicate = Predicates.And( predicate, Predicates.LastNameStartsWith( "M" ) );
falafels.FindAll( predicate );

Output:
Adam Markowitz
Rick Miller

Higher-order functions are a powerful abstraction, and they're only going to become more common and easier to write with C# 3.0 lambda expressions, so give them a try and see how they can help make your code more concise and expressive.

posted on May 31, 2007  #    by Adam Anderson  Comments [0]
 Wednesday, May 23, 2007

Visual Studio comes with some handy (and some not-so-handy) code snippets, but for some reason there isn't one for declaring a method. It isn't that big of a deal, but after a while you might start resenting finishing your method header, then having to type Enter, {, Enter, }, Up, Enter to start the method body. Here's a snippet that will automatically put an open and closing brace after your method declaration, with the cursor ready to go in between:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>method</Title>
      <Shortcut>m</Shortcut>
      <Description>Code snippet for methods</Description>
      <Author>Adam Anderson</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>method-modifiers</ID>
          <ToolTip>Method modifiers</ToolTip>
          <Default>public</Default>
        </Literal>
        <Literal>
          <ID>return-type</ID>
          <ToolTip>Return type</ToolTip>
          <Default>void</Default>
        </Literal>
        <Literal>
          <ID>member-name</ID>
          <ToolTip>Method name</ToolTip>
          <Default>Name</Default>
        </Literal>
        <Literal>
          <ID>param-list</ID>
          <ToolTip>Parameter list</ToolTip>
          </Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[$method-modifiers$ $return-type$ $member-name$( $param-list$ )
{
$end$
}]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

posted on May 23, 2007  #    by Adam Anderson  Comments [0]
 Monday, May 14, 2007

This is one of those little things I'm looking forward to getting out of C# 3.0: inferred types. I've always throught to myself, "I just declared the variable to be that type, can't you figure it out without me spelling it out for you, C# compiler?" Well, the compiler can't figure it out yet, but here's a little snippet I cooked up so at least I don't have to type the type name twice:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>var</Title>
      <Shortcut>var</Shortcut>
      <Description>Code snippet for declaring and initializing a variable with a type cast</Description>
      <Author>Adam Anderson</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Variable type</ToolTip>
          <Default>Type</Default>
        </Literal>
        <Literal>
          <ID>name</ID>
          <ToolTip>Variable name</ToolTip>
          <Default>Name</Default>
        </Literal>
        <Object>
          <ID>value</ID>
          <ToolTip>Initial value</ToolTip>
          <Default>Value