Why I am right!

January 15, 2009

Google Chrome

Filed under: Browsers, Chrome, Google, Microsoft — Shaun @ 10:29 pm

I have been an IE user forever. I have tried Mozilla quite a few times and I really want to like it but I just never could. I read about Google Chrome and figured I would give it a try. If you haven’t used it stop reading right now and download it.

There are a few features I really like.

  1. It opens VERY fast.
  2. The cursor goes right to the address/search bar when it opens and the default home page is actually useful.
  3. The tabs actually work. You can move them around, drag them in and out of windows and close them in one click
  4. Google regularly  updates the browser and you don’t even know. Now I don’t have to wait years for the next version.

It is one of those things that is hard to explain why it is better.. it just is.

Advertisements

May 22, 2008

Debugging "caught" Exceptions

Filed under: Debugging, Microsoft, Technology, Visual Studio — Shaun @ 10:05 pm

For the past week or so I noticed a few strange entries in the Visual Studio “Output” window when debugging to a certain page of our app.

Overflow

Something on my page is throwing and catching an OverflowException. Now it probably didn’t matter what was causing this exception because the application was working fine but sometimes things like this bother me enough that I have to figure it out. Luckily there is an easy way to debug ANY exception that you see in that window. I found the solution here.

Once I setup Visual Studio to catch all exceptions of type System.OverflowException (by following the steps in the linked article) the debugger popped the following window anytime that exception was thrown.

  Overflow

From here I was able to follow the stack trace to debug the actual problem which was an Integer getting set incorrectly in a grid we use.

April 25, 2008

SelectionChanged event fires during databinding (Solution)

Filed under: Microsoft, WPF, XAML — Shaun @ 2:54 am

I was having an issue where the SelectionChanged event would fire during WPF DataBinding. While this may be valid, this was not the desired behavior for my application.

Since I could not find anything after a quick Google search I decided to post my workaround. Part of this example was taken from Adam Nathan’s excellent WPF book. Here is some sample code:

Window1.xaml

<Window x:Class="WpfApplication2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="200" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="dataProvider" XPath="GameStats">
            <x:XData>
                <GameStats xmlns="">
                    <GameStat Type="Beginner">
                        <HighScore>1203</HighScore>
                    </GameStat>
                    <GameStat Type="Intermediate">
                        <HighScore>1089</HighScore>
                    </GameStat>
                    <GameStat Type="Advanced">
                        <HighScore>541</HighScore>
                    </GameStat>
                </GameStats>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource dataProvider}, XPath=GameStat/HighScore}"
                SelectedValue="1203" SelectionChanged="ListBox_SelectionChanged" />
    </Grid>
</Window>

Window1.xaml.cs

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication2
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      ListBox listBox = (ListBox)sender;
      if (listBox.IsLoaded)
      {
        Debug.WriteLine("ListBox Selection REALLY Changed.");
      }
      else
      {
        Debug.WriteLine("ListBox Selection Changed but we don't care!");
      }
    }
  }
}

As you can see I am looking at the IsLoaded property which comes from FrameworkElement. This is not set to true until the data binding is complete. I am not sure if this will work 100% of the time, but it works for my needs.

January 18, 2008

Changing the diff/merge program used by Visual Studio

Filed under: Beyond Compare, Microsoft, Technology, Visual Studio — Shaun @ 9:42 pm

I first want to congratulate Microsoft on releasing a diff tool that is significantly better than any of their previous versions. I would also like to thank the developers for giving us a way to change the default tool to something else.

Personally I prefer using Beyond Compare. There are a number of reasons why I like it better but in the end it just comes down to what you are familiar with and personal preference. If you are still stuck in the stone age using tools like WinDiff, SourceSafe give it a try, it is free for 30 days.

Anyway, here is how you change the default diff/merge tool in Visual Studio. I know this works in 2005 and 2008. From what I recall the menus are pretty similar in each version.

  • Open Visual Studio
  • Click Tools.. Options
  • Select Source Control in the left pane. (If it is not there check the “Show all settings” box at the bottom left.
  • Select Visual Studio Team Foundation Server.
  • Click the “Configure User Tools” button in the right pane.

Options

  • Click Add in the “Configure User Tools” window

Add

Now you are ready to enter the configuration for whichever tool you choose to use. Below is what I entered for Beyond Compare.

Compare Settings:

%1 %2 /title1=%6 /title2=%7

BeyondCompare

Merge Settings:

%1 %2 /savetarget=%4 /title1=%6 /title2=%7

Merge Settings

If you are not using Beyond Compare here is a post by James Manning that lists settings for different compare and merge tools including WinDiff!

 UPDATE (2/20/08): Added Merge settings

January 2, 2008

Updating XML columns in SQL Server 2005

Filed under: Microsoft, SQL, SQL Server 2005, Technology — Shaun @ 7:33 pm

I had a table with a large XML column containing hundreds of rows. I needed to update the value of a single node in some of those records. Here are the steps I used to do it and the errors I encountered along the way. Hopefully this will save someone else some time. I cannot use my actual code for a number of reasons so I will attempt to simplify it for this example.

Assume you have a XML format like this:

<Sample>
  <NodeOne>Value1</NodeOne>
  <NodeTwo>Value2</NodeTwo>
  <NodeThree>OldValue</NodeThree>
</Sample>

We want to replace ‘OldValue’ in NodeThree with ‘NewValue’.

Attempt 1 (Invalid):

DECLARE @newValue varchar(50)
SELECT @newValue = 'NewValue'
UPDATE [Product]
   SET ProductXml.modify('replace value of (/Sample/NodeThree)[1] with '+ @newValue)

Since this script would be run a number of times with a different parameter I needed a way to change the ‘NewValue’ without rewriting the script. I was hoping to use String concatenation but it does not work.

Msg 8172, Level 16, State 1, Line 5
The argument 1 of the xml data type method “modify” must be a string literal.

Attempt 2 (Invalid):

DECLARE @newValue varchar(50)
SELECT @newValue = 'NewValue'

UPDATE [Product]
   SET ProductXml.modify('replace value of (/Sample/NodeThree)[1] with sql:variable("@newValue")')

To fix the above error I had to use the sql:variable syntax. as you can see this simple replaces the string concatenation with sql:variable(“@newValue”). I thought I was home free but there was one more problem.

Msg 2356, Level 16, State 1, Line 6
XQuery [Product.ProductXml.modify()]: The target of ‘replace value of’ must be a non-metadata attribute or an element with simple typed content, found ‘element(NodeThree,xdt:untyped) ?’

Attempt 3 (Valid):

DECLARE @newValue varchar(50)
SELECT @newValue = 'NewValue'

UPDATE [Product]
SET ProductXml.modify('replace value of (/Sample/NodeThree/text())[1] with sql:variable("@newValue")')

What I really wanted to do was replace the text of the NodeThree element. To do that I had to add /text() to the end of the XPath. This script will replace the text of the specified element with whatever value is in the @newValue parameter. Thanks to this thread for the answer.

Disclaimer:

As far as I know this only works on un-typed XML columns. If you have a typed column I believe you need to add a couple things to this statement but hopefully this will point you in the right direction.

During testing I wrapped my entire script in a BEGIN TRANSACTION/ROLLBACK TRANSACTION so I could see the results of my script without doing any damage. You should do at least that before attempting any updates as the XPath is generally tricky.

December 20, 2007

ASP.Net Edit and Continue in Visual Studio 2005/2008

Filed under: ASP.Net, Edit and Continue, Microsoft, Technology, Visual Studio — Shaun @ 4:20 pm

I have seen many articles that said Edit and Continue does not work in ASP.Net for Visual Studio 2005. Well, it works for me and here is how to enable it. I have showed many people how to enable this and every time I show someone I have to figure it out again. So I am putting this out there to hopefully save someone else the frustration.

To enable Edit and Continue there are 2 separate setting. The first is under Tools.. Options. Open the Debug section of the tree and click on the Edit and Continue Group. Make sure “Enable Edit and Continue” is checked. If you are using the VS 2005 Professional you may need to click a checkbox at the bottom of this screen saying something like Show all Options.

Options

The second setting is in a strange place. I am not sure if this works with the new Web Projects introduced in 2005. We are using the Visual Studio 2005 Web Application Project which may have something to do with it. Open up the solution you are attempting to debug, right click on the web project, and click Properties. When the property window opens click down to the “Web”. Check the Enable Edit and Continue box and you should be all set.

ProjectSettings

I will never understand why there are two places you have to enable this, but at least it works.

UPDATE: You must be at a break point in your code to edit. You cannot edit method signatures, add new methods, etc. Changes like that were not what Edit and Continue were designed for and I can’t even start to think how that would be implemented by Microsoft.

UPDATE (again): I recently started working with Visual Studio 2008. This fix applies to that version as well.

Blog at WordPress.com.