# Sunday, May 14, 2006

Reading about the CLR in SQL Server 2005

I'll be doing my SQL Server 2005 session at Software Developer Conference 2005 (www.sdc.nl) on Tuesday morning. Just surfing the web to see what info is out there I stumbled on a great MSDN article by a group of people (Balaji Rathakrishnan, Christian Kleinerman, Brad Richards, Ramachandran Venkatesh, Vineet Rao, Isaac Kunen)

Go here.

#    Comments [0] |
# Tuesday, May 09, 2006

A more complex C# stored procedure

Another example.

public partial class StoredProcedures
{
  [Microsoft.SqlServer.Server.SqlProcedure]
  public static void PerformDomainCount()
  {
    SqlConnection conn = new SqlConnection();
    SqlDataReader reader = null;
    try
    {

    // use "Context Connection=true" to specify that you're
    // tagging along on the current connection
    // if you wish you could connect to an external
    // database
    conn.ConnectionString = "Context Connection=true";

    // You have to open the connection, which feels a little strange
    conn.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select count(*), dbo.ExtractDomain(email) from customers group by dbo.ExtractDomain(email)";
    cmd.Connection = conn;

    reader = cmd.ExecuteReader();
    SqlMetaData[] meta = DefineMetaData();
    SqlDataRecord record = new SqlDataRecord( meta );
    if ( reader.HasRows )
    {
      reader.Read();
      Copy( reader, record );
      SqlContext.Pipe.SendResultsStart( record );
      SqlContext.Pipe.SendResultsRow( record );
      while ( reader.Read() )
      {
        Copy( reader, record );
        SqlContext.Pipe.SendResultsRow( record );
      }
    }

    }
    catch ( Exception exception )
    {
      SqlContext.Pipe.Send( "ERROR: " + exception.Message );
    }
    finally
    { 
      // Close all
      reader.Close();
      conn.Close();
      if ( SqlContext.Pipe.IsSendingResults )
      {
        SqlContext.Pipe.SendResultsEnd();
      }
    }
  }

  private static SqlMetaData[] DefineMetaData()
  {
    SqlMetaData[] meta = new SqlMetaData[2];
    meta[0] = new SqlMetaData( "Count", SqlDbType.Int );
    meta[1] = new SqlMetaData( "Domain", SqlDbType.NVarChar, 50 );
    return meta;
  }

  private static void Copy(SqlDataReader reader, SqlDataRecord record)
  {
    record.SetSqlInt32( 0, reader.GetSqlInt32( 0 ) );
    record.SetSqlString( 1, reader.GetSqlString( 1 ) );
  }
}

#    Comments [0] |

Creating a stored procedure in C#

An example says it all.

[Microsoft.SqlServer.Server.SqlProcedure]
public static void PerformDomainCount()
{
SqlConnection conn = new SqlConnection();
// use "Context Connection=true" to specify that you're
// tagging along on the current connection
// if you wish you could connect to an external
// database
conn.ConnectionString = "Context Connection=true";

// You have to open the connection, which feels a little strange
conn.Open();


SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from customers";

SqlDataReader reader = cmd.ExecuteReader();

// Send result to caller
SqlContext.Pipe.Send( reader );

// Close the connection
conn.Close();
}

#    Comments [0] |

Running C# in SQL Server 2005

If you want to run managed code in SQL Server 2005 you need to change the default setting for CLR Enabled from false to true.

To do this, run:

exec sp_configure 'clr enabled', '1'

reconfigure

 

 

#    Comments [0] |
# Monday, May 08, 2006

A new item for my favourites

I generally don't bookmark too much, between the history in the address bar and Googling there is just little that requires it, today I found a link that is useful to check on a more regular basis though.

Microsoft has a page where you can find all the latest versions for their SDK's.

Link: http://www.microsoft.com/downloads/Browse.aspx?displaylang=en&aud=5&dctypeid=25

#    Comments [0] |
# Monday, May 01, 2006

Article on 'Getting started with WPF' is online

My article on 'Getting started with WPF' has been published in the Software Developer Magazine and can also be read online at www.sdn.nl

#    Comments [0] |

Maine Developer Network

The Maine Developer Network is a new .NET user group. Recently founded we organize a monthly meeting in Augusta, ME.

Interested? Read more on www.maine-devnet.org

#    Comments [0] |
# Saturday, April 15, 2006

Why software patents are a bad idea...

This site show a very good example of why software patents are a bad idea: http://webshop.ffii.org/

Hint: each number in the picture is a patent violation.

#    Comments [0] |
# Wednesday, April 12, 2006

Use MSBuild to build your .NET 1.1 projects

MSBee is an addon for MSBuild which allows you to use MSBuild to build your .NET 1.1 projects.

=== Start Quote ===

Since the release of MSBuild in .NET Framework 2.0, a very frequent customer request has been to provide a means for MSBuild to build .NET 1.1 applications. This demand stems from users who want to use Visual Studio 2005 and .NET 2.0, but need to continue servicing customers who use .NET 1.1.

MSBuild Extras – Toolkit for .NET 1.1 “MSBee” is an addition to MSBuild that allows developers to build managed applications in Visual Studio 2005 that target .NET 1.1.

=== End Quote ===

Link: http://msdn.microsoft.com/vstudio/downloads/tools/msbee/default.aspx

#    Comments [0] |
# Tuesday, April 11, 2006

SOA made easy

I love it when best practices get incorporated into tools to help you actually follow them in an easy and intuitive way. ServiceBAT, released by the Microsoft Patterns & Practices team, seems to bring a bunch of basic principle of Service Oriented Architecture into Visual Studio with a package called the Service Baseline Architecture Toolkit (ServiceBAT) project. 

Edward Bakker has a great post on his blog with demo screenshots.

The download can be found on GotDotNet. It still all very much beta!

WSCF - Schema-based Contract First Web Services by ThinkTecture is another great tool which takes a similar approach, but also improves the WSDL for your web service.

#    Comments [0] |

Rocky Lothka Has An Interesting Quote When He Talks AboutnbspObject Oriented Designs A Hrefhttpwwwtheservers

Rocky Lothka has an interesting quote when he talks about Object Oriented designs:

"Reuse leads to coupling, coupling leads to high cost, high cost leads to anger, and anger leads to the dark side"

Now this is not just true for object orientation, it also applies to (web) services. Applications that use multiple web services and thus depend on multiple external systems are 'coupled'. Although at a technical level technologies like web services make that there is no binary dependency the logical, or business dependency is still very much there.

Headaches to ponder:
- How does my app behave when the web service is unavailable?
- What is the 'business' consequense of the technical solution for your chosen solution?
- How to deal with backup and restore if multiple systems go down?
  Or if just one goes down for that matter.

 

#    Comments [0] |

Enable an application to handle a particular URL protocol

I was just wondering whether it would be difficult to create my own URL like application. I would like to be able to surf to 'myApp:test'.

I guess it starts in the registry.

Here is what I found on MSDN ( http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/pluggable/overview/appendix_a.asp ):

---

To enable an application to handle a particular URL protocol, you must add a new key, with the appropriate keys and values, to the registry in HKEY_CLASSES_ROOT.

The new registry key must match the protocol scheme that is being added. For instance, to add the protocol note:, the key added to HKEY_CLASSES_ROOT should be note. Under this new key, the Default string value should be the name of the new protocol, and the URLProtocol string value should contain either protocol-specific information or an empty string. Also under the new key, a DefaultIcon key and a shell key should be added. The Default string value under the DefaultIcon key must be the file name to use as an icon for this new URL protocol. Under the shell key, a key using a verb (such as open) should be added. A command key and a DDEEXEC key can be added under the key using a verb. The values under the command and DDEEXEC keys are used to call the application.

The following example shows which registry values must be added to register a new application (notepad.exe in this example) to handle a new URL protocol (note:).

[HKEY_CLASSES_ROOT]
    [note]
        (Default) = "URL:Note Protocol"
        URL Protocol = ""
        [DefaultIcon]
            (Default) = "notepad.exe"
        [shell]
            [open]
                [command]
                    (Default) = "c:\windows\notepad.exe %1"

By adding these settings to the registry, attempts to navigate to URLs such as note:c:\myfile.txt would launch Notepad to edit the file c:\myfile.txt. Of course, all the commands supported under Shell\Open are supported, including DDEEXEC (in other words, "command" is not the only key you can put under the verb).

#    Comments [0] |
# Tuesday, March 28, 2006

Download powerpoint

You can now download the powerpoint from my 'Introduction to WPF' session on the download page at www.develop-one.com.

Or download directly from this link: Introduction to WPF.ppt

 

#    Comments [0] |
# Wednesday, March 22, 2006

Windows Vista moves to January 2007

Microsoft has released a press statement announcing that Windows Vista consumer availability has been scheduled for January 2007.

 

Below is the original statement:

REDMOND, Wash. — March 21, 2006 — Microsoft Corp. today confirmed that Windows Vista™, the next generation of the Windows® client operating system, is on target to go into broad consumer beta to approximately 2 million users in the second quarter of 2006. Microsoft is on track to complete the product this year, with business availability in November 2006 and broad consumer availability in January 2007.

Windows Vista will deliver great value to businesses by seamlessly connecting people to information, enabling increased mobile and remote productivity, significantly reducing deployment and support costs, and providing a more secure and compliant desktop platform. For consumers, Windows Vista will bring clarity to the world of personal computing, enabling people to more safely and easily accomplish everyday tasks, instantly find what they want, enjoy the latest in entertainment, and stay connected at home or on the go.

More than half a million customers have received the latest community technology preview for Windows Vista, and have been providing consistent and positive feedback.

“Product quality and a great out-of-box experience have been two of our key drivers for Windows Vista, and we are on track to deliver on both,” said Jim Allchin, co-president for the Platforms & Services Division at Microsoft. “But the industry requires greater lead time to deliver Windows Vista on new PCs during holiday. We must optimize for the industry, so we’ve decided to separate business and consumer availability.”

Because of the way businesses test and deploy software, it makes sense for Microsoft volume licensing customers to receive windows Windows Vista starting in November of this year. Availability for consumers and on new PCs will follow in January.

“We strongly support Microsoft’s decision to prioritize quality in determining the schedule for Windows Vista,” said Todd Bradley, executive vice president of the Personal Systems Group at Hewlett Packard. “A January launch of Windows Vista allows us to execute in a consistent way throughout the holidays, and will provide the right opportunity for a large, exciting launch industrywide after the New Year.”

Said Ron Boire, executive vice president and general merchandising manager at Best Buy, “When people come to our stores to buy a new PC or new software for their PC, we want to be able to offer them a broad set of choices, immediate availability and a great retail experience. We agree with Microsoft that it’s best to do this right — and in this case it’s delivering Windows Vista-based PCs with confidence in January 2007.”

#    Comments [0] |
# Friday, March 17, 2006

Bricks

Rockford Lhotka has a very interesting post about whether or not all developers are equal.

http://www.lhotka.net/WeBlog/AllInAllWereJustAnotherBrickInTheWall.aspx

 

#    Comments [0] |
# Friday, March 10, 2006

Performance of DataSets when using Binary Serialization

Beth Massi has an interesting post about the performance of DataSets when using Binary Serialization.

Read more: http://bethmassi.blogspot.com/2006/01/binary-serialization-of-datasets-in.html

#    Comments [0] |

BindingSource.ResetBindings(..)

I'm working on a WinForm 2.0 window on which I have a DataGridView.

The DataGridView is connected to an ObjectDataSource and the datasource of this ObjectDataSource is pointing to an array of items.

This works nicely, however, the user can use a popup to add an entry to the array. The DataGridView will not automatically detect that the underlying array has changed.

Solution: Call BindingSource.ResetBindings(..) after updating the array. This will raise an event which makes the DataGridView redraw itself.

#    Comments [0] |
# Wednesday, March 08, 2006

Windows Live Search

Microsoft Live Search is the new search engine from Microsoft. Will it be able to nibble at google's marketshare?

We'll see. Go to: http://search.live.com

 

:-)

 

#    Comments [0] |
# Monday, March 06, 2006

WPF ContentModel

With WPF we are far more flexible in creating UI solutions since now almost every control can contain other controls.

The classic example being ofcourse the button.
A button cannot just have a text on the surface of the button, it can just as easily have an image.

Example of button with text.

<Button Name="button1">Just text</Button>

Example of button with image.

<Button Name="button1">
 <Image Source="C:\Documents and Settings\Mark\Desktop\banner.jpg" Name="image1" Width="100"/>
</Button>

Now a lot of buttons will have an image and also some text.
With our HTML background we'll probably attempt:

<Button Name="button1">
 Just text<br/>
 <Image Source="C:\Documents and Settings\Mark\Desktop\banner.jpg" Name="image1" Width="100"/>
</Button>

Wrong! A button can contain controls, but has no 'knowledge' of how to produce a layout for multiple controls.
WPF has special controls that provide layout implementation. These are panels. There are four kinds of panels:
- DockPanel
- StackPanel
- Grid
- Canvas

The same is actually true of the Window control. A Window has no 'default' knowledge of layout. VS2005 will by
default add a 'Grid' control as the root panel.

A StackPanel is easiest to use. It will stack items either horizontally or vertically.
In our button example we would want to use the StackPanel as the child of our Button.

<Button Name="button1">
  <StackPanel Orientation="Vertical">
    <TextBlock>Just text</TextBlock>
    <Image Source="C:\Documents and Settings\Mark\Desktop\banner.jpg" Name="image1" Width="100"/>
  </StackPanel>
</Button>

Notice that we don't need the <BR> tag anymore.
The linefeed is really layout information and our StackPanel is in charge of layouting the content.
If however we have a lot of text, then we could put a newline within the TextBlock.

<Button Name="button1">
  <StackPanel Orientation="Vertical">
    <TextBlock>
      Just text
      <LineBreak/>
      The next line
    </TextBlock>
    <Image Source="C:\Documents and Settings\Mark\Desktop\banner.jpg" Name="image1" Width="100"/>
  </StackPanel>
</Button>

A Grid offers more options and acts much more like a table with rows and columns. I'll show a quick example:

<Grid ShowGridLines="True">
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition/>
    <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
    <RowDefinition/>
  </Grid.RowDefinitions>
  <TextBlock Grid.Column="0" Grid.Row="0">Top left</TextBlock>
  <TextBlock Grid.Column="1" Grid.Row="1">Middle</TextBlock>
  <TextBlock Grid.Column="2" Grid.Row="2">Bottom right</TextBlock>
  <Image Source="C:\Documents and Settings\Mark\Desktop\banner.jpg" Name="image1"
      Width="50" Grid.Column="1" Grid.Row="0"/>
</Grid>

As you can see the content of an actual cell is not placed in the cell, but instead the content is listed below the column and row definitions. I must say that I'm still getting used to this notation.

The DockPanel allows you to dock content to a border of the window.
The Canvas panel performs no layout functionality, you're in charge of all layout matters. Much the way 'regular' WinForms let you do all the layouting.

 

#    Comments [0] |
# Sunday, March 05, 2006

Event bubbling or event routing

WPF introduces EventBubbling. People who have done scripting in Internet Explorer will find this very familiar. The concept is that an eventhandler may not be implemented on a GUI-control, but can also be implemented on the parent of that GUI-control. When an event is fired it will traverse the tree from child to parent to parent to parent until it reaches the top control, usually the window.

Let's look at a demo.

Below is the XAML file for Window1.

<Window x:Class="Demo3.EventHandling.Window1"
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
    Title="Demo3.EventHandling"
    >
    <Grid>
    <Button VerticalAlignment="Stretch" Click="button1_Click"
            HorizontalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="1"
            Grid.Row="0" Grid.RowSpan="1" Margin="80,85,77,121"
            Width="NaN" Height="NaN" Name="button1">
      <StackPanel Orientation="Vertical">
        <Image Source="C:\Documents and Settings\Mark\Desktop\banner.jpg" Name="image1"/>
        <TextBox Name="textbox1" Text="Type your name here!" Height="20" Width="333" />
        <Button Name="innerButton" Click="innerButton_Click">Press me!</Button>
      </StackPanel>
    </Button>
  </Grid>
</Window>

Here is the code.

void button1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Hello " + this.textbox1.Text);
    Button source = e.OriginalSource as Button;
    if (source != null)
    {
        MessageBox.Show("The original source is: " + source.Name);
    }
}

void innerButton_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Hello SDN!");
}

This will look like:


Just click on the various controls. Notice that controls that do not have a 'Click' event do not participate in the routing, but may still pass the event on.

In WPF this concept is called event routing. You can halt the routing of an event by setting

e.Halted = true;

 

 

#    Comments [0] |