# Monday, October 31, 2011

Streaming XML

I just rediscovered this blog post about reading and parsing XML as it becomes available in conjunction with LINQ to XML: http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx

It’s a little dated, but still relevant Smile.

#    Comments [3] |

What’s new in .NET Framework 4.5

Just came across this great picture of what’s new in .NET Framework 4.5 (click for larger version):

WhatsNewNET45-en

#    Comments [4] |
# Friday, October 21, 2011

Custom color in reports : convert color to Hex

I was implementing a client report (RDLC) using the Microsoft Report Viewer control and I wanted to set the background color of a table field based on value from my object source. At first I used the Color.ToKnownColor() method, but discovered that this does not work for all colors. I needed to convert to Hex. Here is the little extension method I used:

public static class ColorExtensions
{
 
    #region -- Data Members --
    static char[] hexDigits = {
     '0', '1', '2', '3', '4', '5', '6', '7',
     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    #endregion
 
 
    /// <summary>
    /// Convert a .NET Color to a hex string.
    /// </summary>
    /// <returns>ex: "#FFFFFF", "#554ECE"</returns>
    public static string ToHexString( this Color color )
    {
        byte[] bytes = new byte[3];
        bytes[0] = color.R;
        bytes[1] = color.G;
        bytes[2] = color.B;
        char[] chars = new char[bytes.Length * 2];
        for ( int i = 0; i < bytes.Length; i++ )
        {
            int b = bytes[i];
            chars[i * 2] = hexDigits[b >> 4];
            chars[i * 2 + 1] = hexDigits[b & 0xF];
        }
        return "#" + new string( chars );
    }
 
}
#    Comments [3] |
# Wednesday, October 05, 2011

Visual Studio 2010: Debugging a x86 WCF service on a x64 machine

I just ran into an issue where I have a WCF service that depends on a .NET assembly that is compiled specifically for x86. In order to use that assembly I need to compile the service as a x86 service. No problem, but now when I want to test or add a service reference to this WCF service I ran into the problem that WcfSvcHost and WcfTestClient both will run a x64 because I’m running Windows 7 x64.

How to solve this? I found the answer in the forums and adapted the answer for my specific problem:

1.Copy WcfSvcHost.exe and WcfTestClient.exe from C:\program files (x86)\Microsoft Visual Studio 10.0\Common7\IDE to a local directory. Keep a backup copy of this file, of course.
2.Start a Visual Studio 2010 Command Prompt (one of the links from the start menu -> Visaul Studio 2010)
3."cd" to the directory where your copy of WcfSvcHost is located.
4.Execute the command "corflags /32BIT+ /FORCE WcfSvcHost.exe"
5.Copy the files back to where you found it.
 
Now your WcfSvcHost and WcfTestClient will be running in 32 bit mode.

#    Comments [3] |
# Friday, September 02, 2011

Omnext @ New York CloudExpo 2011: “If it ain’t Dutch, it ain’t much”

Omnext was part of a Dutch trade delegation visiting the New York CloudExpo 2011.
Here is a link to a great article by Theo Loth (coordinator EuroCloud Nederland) about the event: omnext - cloudexpo 2011.pdf (in Dutch).

If your looking to find out more about Omnext and how Source2VALUE can help improve the ROI of your software development investments, then visit www.omnext.net or talk to Jaco and Andre at the 2011 European Outsourcing Summit in Madrid, Spain.

#    Comments [3] |
# Friday, August 26, 2011

SDN Conferences 2011

For those who will be in the Netherlands in October: the SDN is organizing it’s annual 2-day conference on October 3rd and 4th, 2011. A preview of the schedule is now online and registration has just opened: http://www.sdn.nl/SDN/SDNEvent/SDNConferences2011/tabid/162/Default.aspx

#    Comments [0] |
# Monday, August 01, 2011

Maine Code Camp #2 - Video Impression

A (short) video impression of Maine Code Camp #2:

#    Comments [2] |
# Monday, July 11, 2011

LINQ: Convert list to a dictionary of lists

Suppose I have a list of time cards from multiple employees, but I want to group them into dictionary, based on the Social Security Number (SSN) of the employee. Here is an example of how to convert a list of items into a dictionary of lists:

Dictionary<string, List<TimeCardInfo>> dict;
 
dict = ( from timecard in listOfTimeCards
         group timecard by timecard.SSN ).ToDictionary( x => x.Key, x => x.ToList() );
#    Comments [3] |
# Friday, July 08, 2011

Shrink SQL Server 2008 R2 log files

I just discovered that if you run SQL Server on your developer box and never bother to cleanup your log files, then perhaps they get to be a little big Smile.
Here is a script to quickly shrink a log file:
 
 
Use [YourDatabaseName]
Go
 
select name,recovery_model_desc from sys.databases
GO 
Alter database [YourDatabaseName] SET RECOVERY SIMPLE
GO
Declare @LogFileLogicalName sysname
select @LogFileLogicalName=Name from sys.database_files where Type=1
print @LogFileLogicalName
 
DBCC Shrinkfile(@LogFileLogicalName,100) 
#    Comments [1] |
# Saturday, July 02, 2011

Entity Framework – Model First: Generating DDL for Complex Types

In the model below the phone number for an artist is actually a complex type (a little over engineered, I know, but I was just exploring how well this works).

image

The complex type consists of 4 ‘fields’: CountryCode, AreaCode, Number and Extension:

image

Each ‘field’ has properties set:

image

The great part is that this is fully supported by the DDL generator, so right click on the Entity Framework Designer in Visual Studio 2010 and choose ‘Generate Database From Model…’ and the ‘Artist’ table will be generated as:

 
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
 
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
 
-- Creating table 'Artists'
CREATE TABLE [dbo].[Artists] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(100)  NOT NULL,
    [IsIndividual] bit  NOT NULL,
    [Phone_CountryCode] nvarchar(4)  NOT NULL,
    [Phone_AreaCode] nvarchar(5)  NOT NULL,
    [Phone_Number] nvarchar(10)  NOT NULL,
    [Phone_Extension] nvarchar(10)  NULL
);
GO
-- <snip other tables>
 
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
 
-- Creating primary key on [Id] in table 'Artists'
ALTER TABLE [dbo].[Artists]
ADD CONSTRAINT [PK_Artists]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO
 
-- <snip other primary and foreign key>
 
 
-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
Notice that by default each column is prefixed with the name of the complex type, this is of course needed to ensure column names stay unique across multiple complex types in a single entity.
#    Comments [0] |
# Wednesday, June 29, 2011

Logging and tracing with Entity Framework

If you’re using the Entity Framework and don’t want to implement ToTraceString() at strategic points in your code in order to see what kind of SQL statements are getting generated then check out one of the following:

Update 07-02-2011: Added SQL Server Profiler after comment by Tony Sneed.

#    Comments [2] |

Entity Framework - Model First: One-to-One relationship

If you’re doing Model First with Entity Framework you may run into a scenario where you want to design a 1-to-1 relationship, but also have a foreign key be available on your entity. The default behavior in Entity Framework when doing a 1-to-1 relationship (with Model First), or 1-to-0..1 relationship, is to create a foreign key on the 0..1 side of the association but hide the foreign key. A better approach is this:

  • Start a new model
  • Add the entity that goes on the 1 side of the association, give it a primary key of type integer. Make sure the StoreGeneratedPattern property is set to Identity.
  • Add the entity that goes on the 0..1 side of the assocation, name the primary key as you would a foreign key, make it of type integer. Make sure the StoreGeneratedPattern property is set to None.
    image
  • Click on the 1 entity and add an association, make it a 1-to-0..1, example:
    image
  • Double click the association and set a referential constraint.
    image
  • Now you’re ready to generate your database schema.
#    Comments [0] |
# Monday, June 20, 2011

SQL Server: Getting table size for all tables

Here is a script for getting size information on every table in your SQL Server database. Based on a script by Mitchell Sellers, this script also works if you have multiple schemas in your database:

DECLARE @TableName VARCHAR(100)    --For storing values in the cursor
 
 --Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
 FOR 
SELECT '['+SCHEMA_NAME(schema_id)+'].['+name+']'
 AS SchemaTable
 FROM sys.tables where type_desc = 'USER_TABLE'
FOR READ ONLY
 
--A procedure level temp table to store the results
CREATE TABLE #TempTable
 (
     id int identity,
     tableName varchar(100),
     numberofRows varchar(100),
     reservedSize varchar(50),
     dataSize varchar(50),
     indexSize varchar(50),
     unusedSize varchar(50)
 )
 
--Open the cursor
OPEN tableCursor
 
--Get the first table name from the cursor
FETCH NEXT FROM tableCursor INTO @TableName
 
--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
     --Dump the results of the sp_spaceused query to the temp table
     INSERT  #TempTable
         EXEC sp_spaceused @TableName
     UPDATE #TempTable set tableName = @TableName where id = @@identity 
     --Get the next table name
     FETCH NEXT FROM tableCursor INTO @TableName
END
 
--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor
 
--Select all records so we can use the results
SELECT * 
FROM #TempTable
ORDER BY tableName
 
--Final cleanup!
DROP TABLE #TempTable
 
GO
#    Comments [0] |
# Monday, June 13, 2011

Maine Code Camp #2

The dates are set and confirmed! We have the Mt. Blue Campground booked for July 29th and July 30th. We'll be tenting, presenting and hanging out! If you're interested in speaking or attending the second Maine Code Camp then please email: mark.blomsma@maine-devnet.org. We'll be tenting, presenting and hanging out! More information to follow!

Update: Just to be clear, the code camp will be Friday to Sunday from the 29th to the 31st, with two nights spend at Mt. Blue State Park.

#    Comments [0] |
# Thursday, June 09, 2011

Madam Silverlight

Just wanted to call out attention to a new Silverlight site created by Maine’s own Carolyn Smith: www.silverlightmadam.com

“Silverlight Madam is the creation of Carolyn Smith. For many years Carolyn's preferred medium was watercolors. Then she discovered computer graphics and now she specializes in creating art in Silverlight.”

Note: Carolyn also runs the site www.pixycolors.com.

#    Comments [0] |
# Tuesday, June 07, 2011

Fixing Skype [Windows]

I just had Skype crashing on me every time it tried to connect and go online. The following worked for me (on Windows 7):

1. If the Skype icon is displayed in the system tray at the bottom right of the screen, right-click it and select Quit.
2. Click Start, type "run" and press Enter. (On Windows XP: Click Start and then Run.)
3. Type "%appdata%\skype" and click OK.
4. Locate and delete the file shared.xml. The file may be displayed as shared if file extensions are not displayed by default on your computer.
5.If you cannot find this file:
-> Click Start, type "run" and press Enter. (On Windows XP: Click Start and then Run.)
-> Type "control folders" and click OK.
-> In the View tab, ensure that Show hidden files and folders is enabled.
-> Repeat the instructions from the beginning.
6. Restart Skype.
7. Choose ‘Check for Updates’ from the ‘Help’ menu. Perform upgrade to the latest version.

#    Comments [0] |
# Thursday, May 05, 2011

IT Outsourcing to Bangladesh

Gartner is mentioning Bangladesh as a country that is growing when it comes as a place for outsourcing IT. Omnext was part of a Dutch trade delegation that went down to Bangladesh to explore opportunities. This article (in Dutch) has the details. Omnext delivers software and services to create an MRI-scan of your software. This can be used in outsourcing projects to measure progress, quality and complexity.

#    Comments [1] |
# Monday, May 02, 2011

Augusta Business Intelligence Event, May 16th, 2011

On May 16th the Maine Developer Network will be hosting the Augusta Business Intelligence Event and you're invited to attend.

Thanks goes to John Gagnon for putting together a great event! Here is the tentative agenda for the day:

8:30am - 9:00am Registration (and Breakfast if we find a sponsor)  
9:00 am - 10:00 am Designing the Business Process Dimensional Model  
10:10 am - 11:30 am ETL System Physical Design -
11:40 am - 12:40 am Lunch
12:40 pm - 2:00pm Designing the Analysis Services OLAP Database - Slava Kokaev
2:10 pm - 3:20pm  Interactive Dashboards with PerformancePoint 2010 - Sunil Kadimdiwan
3:30 pm - 4:30pm Custom BI Applications and Interactive Dashboards -  Slava Kokaev


Presenters:
Slava Kokaev - Principal BI Developer/Architect at Industrial Defender, specializing in Design and Development of Business Intelligence Systems, Custom BI Web Applications and BI Integration. Slava is a Boston Microsoft Business Intelligence User Group leader, SQL Server Community and INETA Regional speaker.

Sunil Kadimdiwan - has 25+ years' experience in architecting and implementing database solutions. He has deep knowledge of the Microsoft SQL Server and Business Intelligence technology stack. He is a frequent speaker at Microsoft sponsored code camps and user group meetings in the New England region.

The event will be held at:
Central Main Commerce Center
Florian Auditorium
45 Commerce Drive
Augusta, ME

Go to www.maine-devnet.org/Home/SignUpForEvent.aspx to sign up and find for more information.

#    Comments [0] |
# Thursday, March 31, 2011

Example of TaskSchedular.UnobservedTaskException in action

Today I had a little bit of challenge trying to demonstrate the behavior of TaskSchedular.UnobservedTaskException. TaskSchedular.UnobservedTaskException can be used to deal with any unhandled exception that occurs during the execution of a task. The event fires when the garbage collector tries to clean up the task. My initial code was good, but I forgot to wait for the task to be done before forcing the GC to do its job Smile

The code below is an effective example of how TaskSchedular.UnobservedTaskException works.

1: public  class  Example 
2: {
3:     public  void  Test()
4:     {
5:         Task  t = new  Task ( delegate 
6:         {
7:             Console .WriteLine( "Do test, just before exception."  );
8:             throw  new  Exception ();
9:         } );
10:         t.Start();
11: 
12:     }
13: }
14: 
15: class  Program 
16: {
17:     static  void  Main( string [] args )
18:     {
19:         TaskScheduler .UnobservedTaskException += new  EventHandler <UnobservedTaskExceptionEventArgs >( TaskScheduler_UnobservedTaskException );
20: 
21:         Example  example = new  Example ();
22:         example.Test();
23: 
24:         Thread .Sleep( 2000 ); // delay is needed to make sure the task is done before calling GC. 
25:         Console .WriteLine( "Done sleeping"  );
26: 
27:         GC .Collect();
28:         GC .WaitForPendingFinalizers();
29: 
30:         Console .ReadLine();
31:     }
32: 
33:     static  void  TaskScheduler_UnobservedTaskException( object  sender, UnobservedTaskExceptionEventArgs  e )
34:     {
35:         Console .WriteLine( "Error."  );
36:         e.SetObserved();
37:     }
38: }
39: 
#    Comments [0] |
# Thursday, March 17, 2011

Better code through code contracts

I’ll be speaking at the Bangor Developers meeting next week (March 22nd) about code contracts. The session is all about improving code reliability and predictability using .NET code contracts. Additionally Mark will demonstrate that using a tool like code contracts static analysis essentially helps you do a thorough code review of your own code. If there is time he’ll also do a demo of the Visual Studio Productivity Power Tools.

#    Comments [0] |