Friday, October 30, 2009

Reconnect SQL user after restore

Have you ever restored a SQL database with a SQL login to a new server and not been able to use the SQL login. So you log into Management Console to create the login, which you can but when you try to give it rights to the database, it says "User, group, or role already exists in the current database."

There is a simple way to fix this. Create user in SQL, connect as sa or admin, select the database you just restored and execute

exec sp_change_users_login 'Auto_Fix', 'yourUserName'

You should see a message like.

The row for user 'yourUserAcct' will be fixed by updating its login link to a login already in existence.
The number of orphaned users fixed by updating users was 1.
The number of orphaned users fixed by adding new logins and then updating users was 0.

Tuesday, October 6, 2009

Correct way to Load a window in WPF

Here is the correct way to load a window in WPF. Notice the hooking the Loaded event in the constructor.

public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Window_Loaded);
//MyBigRoutine(); -- Don't do this.
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
MyBigRoutine();
}

Call to MessageBox.Show in App() prevents StartupURI

Not sure what is going on here but a MessageBox.Show in the ctor of WPF's app.xaml.cs prevents the call to the startupURI object. Removed the call to Show and all is well.

Monday, August 31, 2009

Fixed C000021a BSOD

I am sure there are a myriad of reasons for the deadly C000021a BSOD. In my case I had installed the Exchange management tools in an attempt to update Active Directory Exchange information on my local machine. The install failed and when I rebooted a day or two later it blue screened. When I went to uninstall a KB patch to see if that would fix it, I saw Exchange was running on my machine. I left the patch installed, removed the partial installation and the BSOD was banished.

As always when something crashes ask yourself, "What changed." Answering that question is the shortest route to fixing confusing problems.

Tuesday, August 25, 2009

ItemsControl not showing items

This is simple but easy to forget.

To get data bound to WPF controls you frequently use its DataContext. That works for an ItemsControl but what you probably want is ItemSource instead of DataContext. ItemSource will display a collection whereas DataContext only gives you the first item.

Wednesday, July 1, 2009

True after all

A user told me "I deleted this document image but I should not have been able to do that." Perplexed I checked my code.

private bool OkToDeleteScannedPage()
{
   return true;
}

I replied, "Let me get back to you, I think I know what the problem is." :-)

Wednesday, June 24, 2009

What is the Adorner Layer?

I attended some WPF training in Chicago and talked with one of the presenters, Karl Shifflett. He give the most simple explanation of what the adorner layer is.

It is a layer on top of controls that allows you to decorate them without affecting their layout.

A simple example might be placing a red border around a textbox to highlight an error. If this is done at the adorner layer you will see the red highlight but it will not affect the position of the controls underneath.

WPF Converter not firing on NULL

Yesterday I spent some time tracking down a problem with a converter. I was trying to show an icon when the ReviewReasonId, int property of an object had a non-zero value. I was passing ReviewReason.ReviewReasonId into the converter. The problem was when ReviewReasonId was null the converter was not getting called at all.

< Image Grid.Column="0" Source="Resources/ExclamationSign.ico"
Visibility="{Binding Path=ReviewReason.ReviewReasonId, Mode=OneWay,
UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource ReviewReasonVisibilityFormatter}}"/>


Finally I realized that if ReviewReason is null, as opposed to ReviewReasonId, the converter is not called. I simply removed .ReviewReasonId, bound to ReviewReason instead, cast the object as a ReviewReason and checked for the ReviewReasonId property and all is well.

Thursday, June 18, 2009

WPF MVVM Training

I just attended Microsoft's WPF MVVM training in Chicago. Got up at 5:30AM in Columbus, OH, arrived at the training right on time at 9am and didn't even get lost without a GPS.

I am pretty familiar with WPF so I skipped the first day, which was a lap around WPF and attended the MVVM session. What a brain cramming day! I couldn't even send a text without getting left behind, that's how fast it moved but it built precept upon precept so if you listened it all hung together.

Shifflett and Rodriguez were masters of their trades and came with a great depth of information. Shifflett was very entertaining as a speaker and of course for him all things hard are a "No brainer!"

Now it's time to sit down and develop a strategy to apply what was taught.



Karl was kind enough to stay behind and talk with us after the session and take photos.

Friday, May 29, 2009

Testing is good

Today is a good day. I wrote my first real tests. Modules with interfaces that allow for mocking, stubbing and successful results!

It may sound trivial but the transition from the daily grind of churning out features to quality and reliability is not a trivial one.

Thanks to Kris, Jon and Matt from Quick Solutions for helping educate me.

Friday, May 22, 2009

WPF INotifyProperty not working.

If your OnPropertyChanged is not working make sure your Model (what holds the data you bind to) implements INotifyPropertyChanged.

Thursday, April 23, 2009

Simple nHibernate call to SQL GetDate

I needed to get the date from my SQL server. Found this worked fine.

public DateTime GetDate()
{
var session = GetCleanSession();
var query = session.CreateSQLQuery("SELECT Getdate();");
DateTime results = (DateTime)query.UniqueResult();

return results;
}

Wednesday, April 8, 2009

Binding Radio buttons in XAML

Here's how to to bind a radio button to a list, setting both the button and text portions of the radio button.


    public class ReviewReason : NHibernateEntityBase<ReviewReason>
{
public override int Id
{
get { return ReviewReasonId; }
}

public int ReviewReasonId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool DisplayToUser { get; set; }
public bool Checked {get; set;}
...




Loading the DataContext...

    public partial class ReviewReasonView : IReviewReasonView
{
public ReviewReasonModel Model { get; set; }
public IReviewReasonController Controller { get; set; }

public bool? ShowDialog()
{
this.DataContext = Model;
return base.ShowDialog();
}

...


and the XAML binding...

        <ItemsControl ItemsSource="{Binding Path=ReviewReasons, Mode=TwoWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<RadioButton Name="ReviewReason" GroupName="ReviewReasonGroup"
IsChecked="{Binding Path=Checked}"
Content="{Binding Path=Description}"></RadioButton>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>