Wednesday, September 1, 2010

DP_DefaultValueTypeDoesNotMatchPropertyType Error

If you create a Silverlight (and probably WPF) usercontrol and get a DP_DefaultValueTypeDoesNotMatchPropertyType error when rendering the control, make sure you are setting the right kind of default value in the .Register method.

The following code generated that error.

public static readonly DependencyProperty HourFillProperty = DependencyProperty.Register(
"HourFill", typeof(int), typeof(ucScheduleHour),
new PropertyMetadata(null,
new PropertyChangedCallback(ChangeValue)));


My value was an int so I needed to change null to a 0,


public static readonly DependencyProperty HourFillProperty = DependencyProperty.Register(
"HourFill", typeof(int), typeof(ucScheduleHour),
new PropertyMetadata(0,
new PropertyChangedCallback(ChangeValue)));

Wednesday, March 31, 2010

SQL WHERE and JOIN clauses

A brief discussion of the difference in SQL between criteria in the WHERE and JOIN clauses.

Suppose we have a very simple system of two tables A & B. A is a list of Deptartments and B is a list of charges to a departmental account.

Table A
Dept
10
20
30

Table B
Dept Acct Amt
10    15    2
10    11    4
20    11    1

Management request a report that shows all Depts in table A, regardless of the entries shown in B, and the sum of Amount by Acct for dept 15.

You might be tempted to write the SQL as:

SELECT A.DEPT, B.ACCT, SUM(Amount) as TOTAL
FROM A LEFT OUTER JOIN B
ON A.DEPT=B.DEPT
WHERE ACCT = 15 OR ACCT IS NULL
GROUP BY A.DEPT, B.ACCT

However this will give you these results.

Dept Acct Amount
10    15    2
30    NULL  NULL

Dept 20 does not appear in the list. As you can guess from the sample data Dept 20 is eliminated by the where clause, although the
DEPT IS NULL allows Dept 30 to appear.

There is a simple fix to show all departments, move the criteria to the JOIN.

SELECT A.DEPT, B.ACCT, SUM(Amount) as TOTAL
FROM A LEFT OUTER JOIN B
ON A.DEPT=B.DEPT AND ACCT = 15
GROUP BY A.DEPT, B.ACCT

This yeilds

Dept Acct Amount
10    15    6
20    NULL  NULL
30    NULL  NULL

Exactly what was requested.

Here is a little SQL which demonstrates the results.

SELECT A.DEPT, B.ACCT, SUM(AMT) AS AMOUNT
FROM
(SELECT 10 AS DEPT
UNION
SELECT 20
UNION
SELECT 30 ) A
LEFT OUTER JOIN
(
SELECT 10 AS DEPT, 15 AS ACCT, 2 AS AMT
UNION
SELECT 10, 11 , 4
UNION
SELECT 20, 11 , 1
) B
ON A.DEPT=B.DEPT AND B.ACCT=15
--WHERE B.ACCT=15 OR B.ACCT IS NULL
GROUP BY A.DEPT, B.ACCT

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.