John Stagich's Blog

Microsoft .Net Developer

February 2014 Quick Hits

clock February 20, 2014 10:04 by author JohnStagich
  • I was having a problem with my HP Photosmart C7250 printer going offline and staying offline, even though the printer was powered on and the network connection was good.  Here is a link to a utility program from HP (HP Print and Scan Doctor) that fixed the problem.

  • Here is a link to a Visual Studio tool that will recursively collapse selected nodes in the Solution Explorer window.  This tool is handy when you have a Visual Studio solution with a lot of projects and folders.

  • Here is a link to a Visual Studio magazine article about a new Visual Studio 2013 add-in that allows you to search for code snippets from the cloud and then insert them into your code without leaving Visual Studio.  To download the add-in, follow this link.

  • I was working at a site that had a Microsoft SQL 2008 database that was local to the desktop, and was consuming a lot of disk space due to the size of the log file (.ldf) for the database.  Here are three steps to shrink the log file.

1) From within SQL Server Management Studio, right mouse click on the database and select Properties.  In the Database Properties dialog box pick Options.  In the Options dialog box change the recovery model for the database from Full to Simple, and click OK.

2) From within SQL Server Management Studio, right mouse click on the database, and select Tasks -> Shrink -> Files. In the Shrink File dialog box, set the File Type to Log, and click OK.

3) If you want, change the recovery model back to Full from Simple in the Database Properties -> Options dialog box.

December 2012 Quick Hits

clock December 21, 2012 08:55 by author JohnStagich


Here are two WPF gems I found on the Internet this month.

1) For me, one of the drawbacks of the MVVM architecture was the complexity involved generating a message/dialog box from the View Model.  I found this solution on the stackoverflow website that uses Func and Action delegates, which I believe, simplifies the process.

In my View Model:

// Declare the delegates

public Func<string, string, bool>   OkCancelDialog              { get; set; }

public Action<string, string>       MessageDialogInformation    { get; set; }

public Action<string, string>       MessageDialogError          { get; set; }


In my View loaded event, assign MessagBox.Show method to the delegates:

void View_Loaded(object sender, RoutedEventArgs e)


       var vm = new ViewModel(this);

vm.OkCancelDialog           = (msg, caption) => MessageBox.Show(msg, caption, MessageBoxButton.OKCancel) == MessageBoxResult.OK;

vm.MessageDialogInformation  = (msg, caption) => MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Information);

vm.MessageDialogError        = (msg, caption) => MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Error);        

       this.DataContext = vm;


In my View Model, call the delegates:

if (OkCancelDialog("Message", "Caption"))

    //do something if true


    //else do something if false

MessageDialogInformation("Information Message", "Caption"); 

MessageDialogError("Error Message", "Caption");

2) Here is a routine I found on codeplex to allow for only one instance of a WPF application:








March 2012 Quick Hits

clock March 16, 2012 09:28 by author JohnStagich
  • Here is a link to SQL Server Data Type Mappings documentation that I found helpful in understanding how SQL data types map to .NET data types and what SQL DataReader typed accessor (e.g., GetString and GeBoolean) is needed to read a particular SQL data type from within .NET.

  • A good link on how the retrieve the Microsoft SQL rowversion/timestamp value from the standard IDataReader interface.  Below is how I implemented.

    byte[] rowVersionBuffer = new byte[8];
    dataReader.GetBytes(rowVersionField_OrdinalValue, 0, rowVersionBuffer, 8);  // rowversion storage size is 8 bytes.  timestamp is a synonym for rowversion.
    businessObject.RowVersion = rowVersionBuffer;

    // business object rowversion/timestamp property

    private byte[] _RowVersion
    public byte[] RowVersion
       get { return _RowVersion;}
       set { _RowVersion = value; }

  • jsfiddle  A tool to test HTML, CSS, and JavaScript.

.NET Compile Problem: Referenced assembly could not be resolved

clock March 6, 2012 10:31 by author JohnStagich

I was working on a Visual Studio 2010 VB.Net Windows Service application.  I added a reference to an in-house IO library and then used the Imports keyword to import the namespace from the referenced assembly.  IntelliSense recognized the namespace and I had access to the methods in the IO library.

I then tried to compile the application.  It failed.  My Imports statement for the IO library and the methods I used with that library generated error messages.  Along with the errors, I noticed a warning message.  The warning message provided the clue to the solution.  It mentioned that the referenced IO assembly could not be resolved because it had a dependency on System.Data.OracleClient.

Here is the link where I found the solution (thanks Xiaoyu!).  It turns out that when I added the new Windows Service project, the targeted framework was for the .NET Framework 4 Client Profile.  That framework does not include the System.Data.OracleClient.dll. 

The fix: Change the target framework to .NET Framework 4 (Project Properties -> Compile tab -> Advanced Compile Options… -> Target framework drop down).


January 2012 Quick Hits

clock January 13, 2012 11:38 by author JohnStagich
  • How to remove a .Net project/solution from Team Foundation Server (TFS) source code control?  From within Visual Studio 2010, File->Source Control->Change Source Control..., and then unbind the project/solution.  Here is the link where I found this information.

December 2011 Quick Hits

clock December 22, 2011 12:31 by author JohnStagich
  •  Here is another way to clear browser cache with Internet Explorer: F12, Ctrl+R


  • I wanted to include the build date in an "about" form for a Windows desktop application.  Below is code to get the build date of the application.

    ' Get Build Date information.

Dim _ExecutingAssemblyName As String = Assembly.GetExecutingAssembly.GetName().Name
Dim _ExecutingAssembly As Assembly = Assembly.GetExecutingAssembly

  Dim _buildDate As Date = File.GetLastWriteTime(_ExecutingAssembly.Location)
  lblBuildDate.Text = lblBuildDate.Text +
" " + _ExecutingAssemblyName + ", " + _buildDate.ToString
lblBuildDate.Text = " Error trying to find Build Date."


  •  Here is a link to a free interactive .NET tutorial from Motti Shaked.

July 2011 Quick Hits

clock July 29, 2011 13:00 by author johnstagich
  • Here is a link to David Padbury’s blog on using Isotope with Knockout.js.  Check it out!

  • This issue has been covered before in other blog posts, but I want to reiterate what you can do when you get the following error message when working with Microsoft SQL Server: The database could not be exclusively locked to perform the operation.  (I received this message when I tried to change the name of a database.)

A fix for this problem is to set the restrict access property of the database to a SINGLE_USER.  Next, make your change (In my case I changed the name of the database).  Followed by, setting the restrict access property back to MULTI_USER .

You can set the restrict access property through either T-SQL code or from SQL Server Management Studio.

T-SQL Code:


SQL Management Studio:

Right mouse click on the database and select properties.  Under Select a page, click on Options.  Scroll down to the State Group and find the Restrict Access property and change its setting accordingly.

Here is the link where I found this fix.

  • A link to Orchard CMS.  Orchard CMS is a free, open source content management system that allows users to rapidly create web sites on the Microsoft ASP.NET platform. It is built on a flexible extensibility framework that enables developers and customizers to provide additional functionality through extensions and themes.

June 2011 Quick Hits

clock June 14, 2011 09:44 by author johnstagich
  • I had a request to have a company logo that was incorporated into a larger image contain a link to a new company website.  The solution: Create an HTML image map and refer to that image map in the <img> tag.  I used Paint.Net to get the coordinates of the logo for the coords attribute of the <area> tag.  For more details, follow this link.  Thanks Nebojsa Pajkic!

<map name="CompanyLogo">

  <area shape="poly" coords="32,92,89,65,147,96,87,122,48,114" href="" />


<img src="images/largeImageWithLogo.jpg"  usemap="#CompanyLogo">

  • Here is a link on how to optimize WPF performance.  Here is link for a performance profiling tool for WPF from MSDN.

Mimic/Simulate Merge Command in SQL 2005

clock April 5, 2011 10:12 by author johnstagich

We had in place a daily process where we imported item information from SAP into an Items table in a SQL 2005 database.  The SAP information was placed in a .txt file and then bulk inserted into the table using the T-SQL Bulk Insert command.

We were then asked to modify this process.  Instead of completely overwriting the Items table in the database, we needed to merge the SAP item information into the Items table.  That is, we needed to update and insert rows in the Items table using the SAP .txt file as input.

If the database was an SQL 2008 database, we would have used the Merge command to merge the information; however, we were using SQL 2005.  Below are the steps and T-SQL I created to simulate the Merge command.

1)      Created a replica of the Items table called Load_Items

2)      Ran the following T-SQL

-- Load SAP Item information into Load_Items table with Bulk Insert command.

Truncate table Load_Items  

FROM  '\\servername\SAP_Items.txt'
WITH (FORMATFILE = ‘\\servername\SAP_Items.FMT')

   Merge updated and new Item information into Items table by deleting rows in the Items table that have the same key value (Item_Code) as that
   in the Load_Items table and then insert those rows back (update) plus any new rows (insert) from the Load_Items table.

Delete from Items
Where Item_Code In (Select Item_Code from Load_Items)

Insert Into Items
Select * from Load_Items

Argument for a Natural Primary Key over a Surrogate Primary key

clock March 9, 2011 09:53 by author johnstagich

I was querying an Order Header table and was coming up with confusing results (the result set had too many rows).  The table used an Identity column as the primary key.  The table also contained an order confirmation number column that should have been unique.  To make a long story short, the reason I was getting confusing results was because there were rows in the Order Header table that contained the same order confirmation number.  This happened when I imported a more recent version of the Order Header table from another database, but neglected to also import a more recent version of the table that controlled the generation of order confirmation numbers.  When I then created new orders, the new Order Header rows were created using older confirmation numbers, and as a consequence, duplicate order confirmation numbers were entered into the table.  This scenario would NOT have occurred if the order confirmation number was the primary key.  An error would have occurred when I tried to add a new order that had a confirmation number that already existed in the Order Header table.

Here are some links to choosing a primary key that I found interesting.

About the author

I am the owner of Stagich Software Consulting.  Stagich Software Consulting specializes in developing Microsoft .Net/Microsoft SQL software applications.



<<  January 2015  >>

View posts in large calendar

Month List

Category list



The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Sign In