Slides and Demos from Lincoln Users Group Super Laptop Meeting

by jason26. August 2010 11:36

I had a great time speaking at the “Super Laptop Meeting” last night. My 20-minute talk was “Best Practices for Custom WF Activities.” I even won the grand prize, so thanks to all of you that voted for me!

If you are interested in the slides and the demo I showed, you can download them by clicking here. If you want to know more about the Lincoln .NET Users Group, you can do so at http://www.lincolndev.net

Tags:

Development | General | Presentations | WF

Dynamic Control Layout With WPF

by jason11. June 2010 00:27

If you are even a little bit familiar with the Model-View-View Model pattern and its implementation in WPF, you have probably run into the ItemsControl control. Actually, even if you haven’t used MVVM, you may have used it. Somewhat like the Repeater control in ASP.NET, this control allows you to bind to a data source and do something in XAML for each of the items in that data source. In any case, let’s play around with the ItemsControl element in WPF a little bit.

More...

Tags: ,

Development | WPF

Selecting Multiple Rows as a Single Column in SQL

by jason29. March 2010 19:14

Recently, I was writing a report that selected some data out of a normalized SQL Server database. For one of the queries, I needed a “flattened” form of the rows in one of the tables. In essence, what I needed to do was turn several rows of data into a single column.

More...

Tags: ,

Development | SQL

Using LINQ To Find Duplicates

by jason3. February 2010 20:16

I recently ran into a situation that has come up plenty of times for plenty of people. I had an array of integers and I needed to figure out if any of the numbers were in the array more than once.

A variety of solutions ran through my head: sorting, looping, searching, etc. However, LINQ seemed like the most elegant solution. Here’s what I finally settled on:

var duplicates = activityIds.GroupBy(g => g)
.Where(w => w.Count() > 1)
.Select(s => s.Key);
if (duplicates.Count() > 0)
{
errorMessage = "You may only register for a particular activity once";
return false;
}

The first line simply groups the integers by their value. The second and third lines find and select every integer that appears in the array more than once (Count() > 1). In my case, I just needed to know if there were any duplicates, but you can see how you could easily list the duplicated values by iterating over the “duplicates” variable.

This same method can be used to find duplicates across any LINQ to objects – compatible collection and I’ll certainly be using it more in the future.

Tags:

Development | LINQ

A Web Server In Less Than 100 Lines of Code

by jason30. April 2009 15:52

Last night at the Lincoln .NET User’s Group, we had our second laptop meeting. A few people brought some code, a dev tip, or a fully working program to demo. I choose to show my "100 Line Web Server" I created to perform some unit testing on an app that was HTTP-centric, lately.

So, here is the entire program. I hope it’s useful to some and at least interesting to most.

Program.cs:

More...

Tags: , , ,

Development | WCF

Using Command-line Parameters In WPF

by jason31. December 2008 01:25

If you need to read command-line parameters within your WPF application, your first inclination (if you are like me) might be to go looking for the Main method. Of course, you won't find a "Main" method in a WPF application. Instead, start looking at the App.xaml file.

By the way, for the rest of this post, I'll presume you will be using command line switches like the following:

myapplication /key1 value1 /key2 value2

To begin, you will need to add an event handler to the App.xaml file for the Startup event. After that, your App.xaml file might look something like this:

<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml" Startup="AppStartup">
<Application.Resources>
</Application.Resources>
</Application>

(Notice the "Startup" attribute on line 4)

Your event handler's signature (in the App.xaml.cs) should look like this:

void AppStartup(object sender, StartupEventArgs e)
More...

Tags: , , ,

Development | WPF

Save WPF Ink Strokes To a Database

by jason20. December 2008 20:23

In my previous post about creating a bitmap from an InkCanvas in WPF, I mentioned that you could easily save the ink strokes for later. I wanted to post a code example for writing those strokes to a database and de-serialized them back into memory.

The first step in saving to a database is to get an array of bytes from the strokes in the InkCanvas. To do this, the StrokeCollection provides a convenient Save method:

byte[] signature;
using (MemoryStream ms = new MemoryStream())
{
icSignature.Strokes.Save(ms);
signature = ms.ToArray();
}
string sql = "INSERT INTO tblMyTable (mySigCol) VALUES (@Sig)";
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddWithValue("@Sig", signature);
comm.ExecuteNonQuery();

You can just as easily load the contents back from the database by creating a new stroke collection from the bytes save in the database:

string sql = "SELECT TOP 1 mySigCol FROM tblMyTable";
SqlCommand comm = new SqlCommand(sql, conn);
byte[] signature = (byte[])comm.ExecuteScalar();
using (MemoryStream ms = new MemoryStream(signature))
{
icSignature.Strokes = new System.Windows.Ink.StrokeCollection(ms);
ms.Close();
}

If you need to persist a signature or annotation from an InkCanvas to a database and back, the process is very simple and makes the InkCanvas even more useful.

Tags: , , ,

Development | WPF

Consuming JSON Services Without AJAX

by jason15. December 2008 23:59

JavaScript Object Notation (JSON) is one of the technologies used by AJAX and other "Web 2.0" technologies. It is a way to communicate in an object-oriented fashion between the web server and a client-side, JavaScript-based application. Every now and again, you might find yourself in a situation where you would need to consume a JSON service, but without a JavaScript endpoint. I ran into this situation when I needed to talk to an application that exposed a web interface, but has no other API hook. My mind first jumped to "screen scraping" the web pages in the exposed web interface. However, when I started to dive into the HTML, I found a very rich JSON service behind those pages.

An example message that I saw coming back from the application looked like this:

{"build":12639,"label": [
["done",8]
]
,"torrents": [
["74C61EB07A63ED2CBC84B8ECCFF85B1222A8006E",136,"myfile.iso",366779378],
["B28240047FD6C6A7219663AA862B2F1F4DD8AE24",136,"yourfile.iso",366784662]]
,"torrentc": "884739139"}

Examining the message, you'll notice that it seems to be broken into key-value pairs. For instance, the variable named "build" has a value of 12639. More complex object shapes can be represented by arrays like the value of the "label" variable. The above message could be represented in the C# class below:

More...

Tags: , , ,

Development | WCF

Convert WPF InkCanvas to Bitmap

by jason9. December 2008 14:38

We live in a digital age and if you are like me, paper is only used when it is absolutely unavoidable. If you have a tablet PC, signing your name to attach it to a document may be one less thing you will need a physical sheet of paper for.

The WPF ink control can be put into your XAML to collect stylus strokes as a collection of geometry points. Of course, as the stylus moves, the movements are also shown on screen.

inktest

More...

Tags: , , ,

Development | WPF

SQL Bitwise Mask Example

by jason6. September 2007 11:53
DECLARE @SUN INT, @MON INT, @TUE INT, @WED INT, @THU INT, @FRI INT, @SAT INT
SELECT @SUN = 1,
@MON = 2,
@TUE = 4,
@WED = 8,
@THU = 16,
@FRI = 32,
@SAT = 64
'@DayMask may be stored in the database somewhere
DECLARE @DayMask INT
SELECT @DayMask = @SUN @MON @TUE @FRI
Print 'Day Mask: ' + CAST(@DayMask AS VARCHAR(10))
DECLARE @IsSUN BIT, @IsMON BIT, @IsTUE BIT, @IsWED BIT, @IsTHU BIT, @IsFRI BIT, @IsSAT BIT
SELECT @IsSUN = @DayMask & @SUN,
@IsMON = @DayMask & @MON,
@IsTUE = @DayMask & @TUE,
@IsWED = @DayMask & @WED,
@IsTHU = @DayMask & @THU,
@IsFRI = @DayMask & @FRI,
@IsSAT = @DayMask & @SAT
SELECT @IsSUN AS Sunday,
@IsMON AS Monday,
@IsTUE AS Tuesday,
@IsWED AS Wednesday,
@IsTHU AS Thursday,
@IsFRI AS Friday,
@IsSAT AS Saturday

Tags: , ,

Development | SQL

About

Jason Williams is a .NET developer in Lincoln, Nebraska.

The name "Centrolutions" came out of a long search for a domain name. The goal was to create a name that conveyed an ideology of writing software centered (Centr--) on a solution (--olutions) for a particular problem. In other words, it was the only name in a long list that wasn't already registered on the internet.

If you're looking for the products I have for sale, you should go here.