Vince Plaza's Blog

...Microsoft .NET programming ideas and samples

October, 2009

...now browsing by month

 

Sort on the last name in full name field using T-SQL string functions

Tuesday, October 20th, 2009

A friend asked me if it was possible to sort on just a portion of a text field, but not necessarily the first word. For example, lets say you have a full name field that is formattedĀ  as ‘[FirstName] [LastName]‘. There is a simple, if not flawless, way to do this with T-SQL string functions. First, you need to generate a small list of names to work with. I will use a table variable to do this. It is easy to spin up and is disposed of as soon as the query is done running.

declare @MyNames table (FullName nvarchar(20));
insert @MyNames
values ('Betty Ferguson'),('Sam L Jackson'),('Horatio Alger');

Note that I used the much simpler SQL Server 2008 syntax to add the names. If you are using SQL Server 2005, you will need to add the names like this:

insert @MyNames
values ('Betty Ferguson');
insert @MyNames
values ('Sam L Jackson');
insert @MyNames
values ('Horatio Alger');

Since you can’t control people adding a middle initial, I will find the last name by counting the number of characters starting from the right before I find a space character. The best function to use for this is charindex.

select charindex(' ', FullName) as 'LastNameLetterCount' from @MyNames;

The problem is that the charindex function counts from the left. In order to count from the right, I need to flop the text over by using the reverse function. This function will take a string of characters such as ‘My dog spot’ and return it as ‘tops god yM’. I also subtract 1 so that I only return the length of the last name, not the last name and the space.
Click to continue »

“Could not find default endpoint” error with Silverlight, WCF, and MVVM

Friday, October 16th, 2009

I have done this a couple of times, and so thought I would log it so that I remember next time. When I set up a Silverlight project using the MVVM pattern, I prefer to make separate projects for the View and ViewModel sections. I then create a service reference to my data service (WCF) in the ViewModels project. If you have tried this, you may have received a “Cannot find ‘ServiceReferences.ClientConfig’ in the .xap application package.” error message the first time you run the solution. This is caused by the Views project not containing the ServiceReferences.ClientConfig file created in the ViewModels project. No problem, just drag a copy from the ViewModels project into the Views project and you are set to go.

Where I get into trouble is when I reconfigure the WCF service or reference and forget to update the client config file I dragged into the Views project. The error message that gets generated is similar to:

Could not find default endpoint element that references contract ‘ServiceReference.Service’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

Well, I was much happier with the error message telling me the file was missing. With this new message, I end up opening the client config file in the ViewModels project like 6 times looking for a typo, but of course it was auto generated while making the service reference, and so is fine. What I need to do is to replace the ServiceReferences.ClientConfig file in the Views project, and then run the application again. Problem solved.