Subscribe: Keith Barrows
Added By: Feedage Forager Feedage Grade B rated
Language: English
add key  add  background color  blog  color csharpcode  color  csharpcode  net  new  orgunit  sol  sql  string  viewstate 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Keith Barrows

Keith Barrows

Musings on Microsoft Technologies and other technical things


Entity Frameworks 4.1 on SQL Server–some gotchas

Fri, 25 Mar 2011 15:54:12 GMT

I am using MVC3, Visual Studio 2010 SP1 & Entity Framework 4.1.

I was very excited to get my hands dirty with the Code First piece of the new Entity Frameworks.  There are some great articles out there for getting started and a couple of my favorites are:

Armed with the above information I started right in on my first Code First project, more to learn how to rather than create something lasting.  I decided to target a “real” SQL Server as I already had SQL 2005 on my dev machine.  I am also keeping in mind that I want to rewrite my own site ( and that runs on a SQL 2008 shared hosting server.  So, I went into SQL and created a SQL Server based Login with the right privileges.  Now remember, on a shared hosting plan what ever SQL Login you are using will not have rights to drop the database which appears to be one of the things Code First will do when it detects a change. 

Needless to say, I have not got it working with a SQL Login locally so opted to switch to a Trusted Login based on my NT account.  That worked like a charm – the first time I ran the code.  Database built, objects in place, etc.  The next step was seeding some data each time a new build is executed.  (See for how to do this.)  Now we start in on the gotchas!

1) While doing this I had SQL Manager open so I could execute some quick queries to see the data after creation.  When I ran my project I started getting some weird errors and spent about 2 hours tweaking my seed code to figure out what was causing it.  Then I had one of those moments.

  • If you have a SQL window open on the database then Code First bombs as the DB is locked and can’t be dropped.
  • Even if you close the window but the SPID is still spun up in SQL you will get errors.
  • If you have the DB (or any part) actively selected in the tree within SQL Mangler, again, you get errors.

So please, avoid my pain and check your database to see if it is locked!  Code First does not work on a locked database.  It has to be able to drop the DB and recreate it (as I understand it so far).

Some links of interest (varied)

Tue, 04 Jan 2011 18:34:06 GMT


This is a collection of frequently asked questions (and answers) about the Entity Framework (a .Net-based Object-Relational Mapping framework from Microsoft).



MonoDroid is a development stack for using C# and core .NET APIs to develop Android-based applications. MonoDroid will be a commercial product licensed in a similar fashion to our Mono for iPhone product MonoTouch.






This is an open blog for all lovers and purveyors of great craft beers.

Utilizing Generics to make a Class structure more mutable…

Tue, 04 Jan 2011 01:06:55 GMT

While the ASP.NET GridView control supports automatic paging I found it faster to use custom paging in several situations.  I found myself rewriting the same code over and over just to add the basic sorting capabilities to an ASP.NET GridView object.  So today I took just a little bit of time to encapsulate it all into a Class I can use and reuse on any page with a GridView.  In fact, it will probably take longer to write this blog entry than it took to encapsulate the functionality. This set of code is the new class: 1: /// 2: /// Binds a List<T> data source to an ASP.NET GridView object. 3: /// 4: /// Typically a DTO or POCO object. 5: internal class SortingGridView 6: { 7: // - almost there... 8: // - .AsQueryable()... 9:   10: #region Private Declarations 11: private StateBag _viewState = new StateBag(true); 12: #endregion 13:   14: #region Internal Properties 15: internal int TakeIndex { get { return _viewState["takeIndex"].ToString().ToInt32(); } set { _viewState["takeIndex"] = value.ToString(); } } 16: internal int SkipIndex { get { return _viewState["skipIndex"].ToString().ToInt32(); } set { _viewState["skipIndex"] = value.ToString(); } } 17: internal int TotalIndex { get { return _viewState["totalIndex"].ToString().ToInt32(); } set { _viewState["totalIndex"] = value.ToString(); } } 18: internal string SortingField { get { return _viewState["sortingField"].ToString(); } set { _viewState["sortingField"] = value.ToString(); } } 19: internal string SortingDirection { get { return _viewState["sortingDirection"].ToString(); } set { _viewState["sortingDirection"] = value.ToString(); } } 20: internal GridView SortedGridView { get; set; } 21: internal bool CanMovePrev { get; set; } 22: internal bool CanMoveNext { get; set; } 23: internal string NavigationText { get; set; } 24: internal List SourceList { get; set; } 25: #endregion 26:   27: #region Constructor(s) 28: /// 29: /// Use this on all Post Backs 30: /// 31: /// ViewState from the page you are embedding this on 32: /// The target GridView object 33: /// The List<T> Data Source to bind to the grid 34: internal SortingGridView(StateBag viewState, GridView GridView, List SourceDataList) 35: { 36: _viewState = viewState; 37: SortedGridView = GridView; 38: SourceList = SourceDataList; 39: BindGrid(); 40: } 41: /// 42: /// Use this the first time to initialize all the ViewState paramaters. 43: /// 44: /// ViewState from the page you are embedding this on 45: /// The target GridView object 46: /// The List<T> Data Source to bind to the grid 47: /// Default value for LINQ's .Take() 48: /// Default value for LINQ's .Skip() [...]

Extension Methods make life easier (C#)

Thu, 25 Feb 2010 20:41:10 GMT

I use extension methods when I remember they exist.  I started getting tired of writing code to check strings for Null, Empty, Equal before using a database column when I remembered Extension Methods are there to make life easier!  Some great articles/blog entries on it can be found at: Extending Base Type Functionality with Extension Methods – Scott Mitchell, 4 Guys From Rolla New "Orcas" Language Feature: Extension Methods – Scott Guthrie, PM, Microsoft ASP.NET Team In a nutshell I found myself writing code that looks like: 1: if (!string.IsNullOrEmpty(details.FeedSourceTable) && details.FeedSourceTable.Equals("auto", StringComparison.CurrentCultureIgnoreCase)) 2: { 3: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #dcdcdc; margin-left: 15px; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } After creating a (simple) string extension method I now have code that looks like: 1: if (details.FeedSourceTable.IsNotNullEmptyAndEquals("auto", StringComparison.CurrentCultureIgnoreCase)) 2: { 3: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #dcdcdc; margin-left: 15px; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } To accomplish this I added a new class, ExtensionsString, to my root library (Sol3.dll) and added the extension methods to it.  The class looks like: 1: using System; 2:   3: namespace Sol3 4: { 5: public static class ExtensionsString 6: { 7: public static bool IsNullOrEmpty(this string source) 8: { 9: return string.IsNullOrEmpty(source); 10: } 11: public static bool IsNotNullEmptyAndEquals(this string source, string target) 12: { 13: if (!string.IsNullOrEmpty(source) && source.Equals(target)) 14: return true; 15: else 16: return false; 17: } 18: public static bool IsNotNullEmptyAndEquals(this string source, string target, StringComparison compareOption) 19: { 20: if (!string.IsNullOrEmpty(source) && source.Equals(target, compareOption)) 21: return true; 22: else 23: return false; 24: } 25: } 26: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #dcdcdc; margin-left: 15px; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .htm[...]

Using LINQ to SQL Classes in VS2005

Tue, 21 Jul 2009 17:58:27 GMT

I am having a devil of a time setting up LINQ for SQL in VS2005.  I have fx3.5 setup, have installed fx3.5SP1, etc, etc.  What googling has shown me is when I want to add a LINQ to SQL Classes item to my project I should see something like the first image.  Instead, what I am seeing is the second image.



Please note some very fundamental differences between the two images.  I should have Categories, I do not.  I should see LINQ items, I do not…

Any suggestions?  I do not have the option to upgrade to VS2008 yet.

Missed my High School Reunion :(

Fri, 10 Oct 2008 18:59:46 GMT

Wish I had thought to google for this.  Another classmate just left me a message on MySpace and the link to my HS Reunion site

With all of this technology at hand it is still hard to know when periodic events like this happen.  How does everyone else stay abreast of events like this?
Cross posted from my blog at

Loading XML with accented characters breaks System.XmlDocument.Load()

Sat, 27 Sep 2008 00:29:40 GMT

It took me a bit of time but I finally found a solution for loading a XML file that has accented characters (like áéíóúâä) into a UTF-8 format.  I'm loading data for a client and it ended up having a name in it with an accented e character.  For the first time on this project System.XmlDocument.Load() was blowing up.  With a lot of Googling I finally found a link that gave me, what I hope, is the solution for this problem.  For now it is working so I'll go with it.  The link to the article I found is in the code sample below as well as the image to the left.  The magic happens by reading in while enforcing a double-byte encoding then saving out in an encoding that gives the visual representation we, in the US, would expect. Hope this helps someone else. 1: #region Fix Character Encodings 2: // Need to drop accented characters back to normal characters... 3: // reference: // 4: StreamReader sr = new StreamReader(_pfInfo.WorkingFile, Encoding.GetEncoding("iso-8859-1")); 5: string fileContents = sr.ReadToEnd(); 6: sr.Close(); 7: sr = null; 8:   9: StreamWriter sw = new StreamWriter(_pfInfo.WorkingFile, false, Encoding.GetEncoding("iso-8859-8")); 10: sw.Write(fileContents); 11: sw.Flush(); 12: sw.Close(); 13: sw = null; 14: #endregion .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } Cross posted from my blog at[...]

Managed Extensibility Framework (MEF) is on CodePlex

Mon, 08 Sep 2008 17:18:37 GMT

The Managed Extensibility Framework (MEF) is a new library in .NET that enables greater reuse of applications and components. Using MEF, .NET applications can make the shift from being statically compiled to dynamically composed. If you are building extensible applications, extensible frameworks and application extensions, then MEF is for you.

I've been doing a few projects with a plug-in architecture and am going to be playing with this to see how I can benefit from it. Hopefully, I'll post some stuff out here as well on this new journey.

Cross posted from my blog at

SQL Not In Revisited

Wed, 27 Aug 2008 17:52:46 GMT

I always seem to struggle with a NOT IN clause when there are more than one column to compare.  This, of course, comes up when I am trying to get records from one set that are NOT IN another set and the comparison has to be done across multiple columns.  For instance, if I want to compare table 1 (users) against table 2 (importedUsers) and I want to see if the mandatory fields are there and *NOT* work with those that fail I usually ended up doing a cursor.  But - there is a much easier way to do it and it does need a cursor!  Thanks to David Penton for pointing me in this direction!

DECLARE @impUsers TABLE(empID VARCHAR(50), hireDate SMALLDATETIME, userNew BIT, userChange BIT, userTerm BIT)

( [empID]
, [hireDate]
, [userNew]
, [userChange]
, [userTerm]
SELECT i.empID, i.hireDate, 0, 1, 0
  FROM [ImportUsers] i
INNER JOIN Users u ON i.[empID] = u.[empID]
                     FROM [Users] u2
                    WHERE u2.[empID]         = i.[empID]
                      AND u2.[lastName]      = i.[lastName]
                      AND u2.[firstName]     = i.[firstName]
                      AND t2.[countryInfo]   = i.[countryInfo]
                      AND t2.[emailAddress]  = i.[emailAddress]
                      AND t2.[region]        = i.[region])

Sweet, simple and elegant.  The above query will produce a new record in the temp table for each record that has one or more changes across the 6 fields I am comparing in the NOT EXISTS clause.  What is happening inside the parenthesis is the elegant part.  The SQL Query Engine is comparing one row from the Import table to all rows in the Users (2) table.  If it finds a match then it returns 1 - which means it does exist.  This negates the overall WHERE clause and does not insert it into the temp table.  When doing this in a cursor it was taking 30-45 seconds.  As a T-SQL query it takes less than 3 seconds.  That's an order of magnitude faster!

Cross posted from my blog at

SQL XML - TreeView

Mon, 25 Aug 2008 22:52:47 GMT

I sometimes forget how to do an XML output formed in a tree when dealing with a single table parent/child relationship.  There is a great explanation on SQL Server Central on *how* to do this.  I am mainly capturing the link and the SQL I just generated to do this.  This query will drill down 12 levels at the most.  Just alter the case statement (pivot) to go deeper. ALTER PROC getOrgUnitTreeAsXml AS BEGIN     ;WITH OrgUnit1     AS     (         SELECT             0 AS [Level],             [OrgUnitId],             [orgUnitParentID],             [orgUnit],             CAST( [orgUnitID] AS VARBINARY(MAX)) AS Sort         FROM [orgUnit]         WHERE [orgUnitParentID] IS NULL         UNION ALL         SELECT             [Level] + 1,             p.[OrgUnitId],             p.[orgUnitParentID],             p.[orgUnit],             CAST( SORT + CAST(p.[orgUnitID] AS BINARY(4)) AS VARBINARY(MAX))         FROM [orgUnit] p         INNER JOIN OrgUnit1 c ON p.[orgUnitParentID] = c.[OrgUnitId]     )     ,  OrgUnit2 AS     (         SELECT             [Level] + 1 AS Tag,             [OrgUnitId],             [orgUnitParentID],             [orgUnit],             sort         FROM OrgUnit1     )     , OrgUnit3 AS     (         SELECT             *,             (SELECT Tag FROM OrgUnit2 r2 WHERE r2.[OrgUnitId] = r1.[orgUnitParentID]) AS ParentTag         FROM OrgUnit2 r1     )     SELECT Tag, ParentTag as Parent,     CASE WHEN tag = 1 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!1!id',         CASE WHEN tag = 1 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!1!name',     CASE WHEN tag = 2 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!2!id',         CASE WHEN tag = 2 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!2!name',     CASE WHEN tag = 3 THEN [OrgUnitId] ELSE NULL END AS 'OrgUnit!3!id',         CASE WHEN tag = 3 THEN [orgUnit] ELSE NULL END AS 'OrgUnit!3!name',     CASE WHEN tag = 4[...]

Microsoft's Velocity - Caching turned up a notch

Fri, 06 Jun 2008 19:28:41 GMT


Microsoft just released it's CTP-1 of a new caching product code named Velocity.  It is a distributed caching technology that should add extra velocity to your ASP.NET applications.

Summary: “Velocity” is a distributed in-memory application cache platform for developing scalable, available, and high-performance applications. “Velocity” fuses memory across multiple computers to give a single unified cache view to applications. Applications can store any serializable CLR object without worrying about where the object gets stored. Scalability can be achieved by simply adding more computers on demand. “Velocity” also allows for copies of data to be stored across the cluster, thus protecting data against failures. “Velocity” can be configured to run as a service accessed over the network or can be run embedded with the distributed application. “Velocity” includes an ASP.NET session provider object that enables ASP.NET session objects to be stored in the distributed cache without having to write to databases. This increases the performance and scalability of ASP.NET applications (17 printed pages)

Some more resources:

Cross posted from my blog at

VS 2008 and .NET FX 3.5 SP1 (Beta)

Mon, 12 May 2008 19:40:45 GMT

The beta has been released.  Scott Guthrie has a huge list of what to expect.

(The image should link you directly to the download page.)

Cross posted from my blog at


Fri, 25 Apr 2008 23:29:23 GMT


Do you know what happens when you read blogs?  You mine expands, dude!  Like Woaoooo...

Something Scott Hanselman posted caught my attention.  He posted about Alt.Net.  I am now searching out all things ALT.NET to make sure my head was wrapped around the ideas in a way that makes sense to me.  Pass the word...

Cross posted from my blog at

Using multiple config files in one application

Tue, 22 Apr 2008 04:23:29 GMT

I'm supporting an ASP (Application Service Provider) style application for my company.  Instead of putting each client's settings into a single config file, I wanted to have the main config file point to each clients configuration.  After a bit of digging, I came up with a solution that works.  I don't know if it is the best solution but it does work.  My main config file now looks like:
A client config file now looks like: I have a service that will iterate through the appSettings and setup a file handler for each client. The service creates a new ClientInfo object for each client: #region Constructor public Service() { _clientInfo = new Dictionary(); _clientList = new List(); _clientCount = Convert.ToInt32(ConfigurationManager.AppSettings.Get("Clients").ToString()); _environmentName = ConfigurationManager.AppSettings.Get("EnvironmentName").ToString(); for (int i = 0; i < _clientCount; i++) { string appKey = String.Format("Client {0}", i+1); string clientName = ConfigurationManager.AppSet[...]

Latest build of the ASP.NET MVC source on CodePlex

Thu, 17 Apr 2008 08:17:53 GMT


Come and get it!  If you are a MVC developer and really want to dig into the core of MVC then go directly to CodePlex and get the codeScott Guthrie has a LOT more details on his blog.

Cross posted from my blog at

DDD Ireland

Tue, 15 Apr 2008 19:48:15 GMT

Looking for events?  Are you in Ireland on May 3rd?  What better way to spend a Saturday than at an event with some funny charismatic notable characters like Plip and Sussman!  With Plip's permission I've duplicated his blog entry for this event

Developer Event? Ah go on, go on, go on.




Check it out at


Cross posted from my blog at

Getting started for the long haul...

Tue, 08 Apr 2008 19:19:13 GMT

I've worked in everything from startups to multi-billion dollar companies plying my trade as a software engineer (head geek, architect, tester, developer, business analyst, janitor, etc).  There seems to be 2 defining characteristics that define successful ventures and flailing ventures.  One is the individual doing the work and the other is documentation.

How do you go about picking someone to work for you?  Is it a gut feeling or is it an in depth interview process.  It's always a bonus when you find that rock star out of that mound of applicants.  But how do you define rock star in a rapidly evolving industry?  Read Write Web has an interesting article on the top 10 traits of a rock star software engineer

Now that you have your rock star how do you get the most from them?  If your shop is like 80% of the shops I've worked in over the last 20 years, documentation is still on the To Do list, the last guy never finished it before he left or plain not available.  There is nothing like working on a mature system with a database that has nearly 1,000 tables and 5,000 stored procedures.  The UI is running strong at 1,500 pages, 250 reports and a change in direction for the underlying framework - not once but 5 times so far.  I just read an article on Found/Read titled 10 Reasons Why Documentation is a Startup Secret Sauce.  Believe me, not having the documentation extends my time on the order of 10 or 20 times when it comes to large systems.

Cross posted from my blog at

Microsoft Mix 08

Thu, 06 Mar 2008 19:54:27 GMT


If you did not make it to Mix 08 this year the keynote speeches are now available online.  If you don't know what mix is you may want to take a look at: Tags: ,
Cross posted from my blog at