April 2012 - Digital Tool Factory blog April 2012 - Digital Tool Factory blog

The Digital Tool Factory Blog

I bring Blog Prophet into the world

I recently posted the staging site for my new Blog Prophet venture – see who called it with Blog Prophet.  The main thrust of the new company is that it allows bloggers to compete on merit, not longevity and not sleazy seo tactics.


29
Apr 12


Written By Steve French

 

How to programatically set the sql server database used by entity framework

The Problem:

'Entity Framework' photo (c) 2009, SondreB - license: http://creativecommons.org/licenses/by/2.0/For one reason or another you need to set the database used by entity framework programatically.  The more common way of doing such a thing is to set the database in your web.config class, but for whatever reason you need to set it in actual C# code.

The Cause:

There is no cause really, it is merely the way that entity framework and sql server work together.

The Solution:

The answer lies in setting the constructor properly.

Example Code Using Entity Framework and Data Context

using System.Data.Entity;

namespace MyDBServerExperiments.Models
{
public class DTFContext : DbContext
{
public DTFContext() : base(“Data Source=MyDBServer.DBServer.com;Initial Catalog=MyEntityFrameworkDB;Persist Security Info=True;user id=MyUsername; Password=MyPassword;”) { }

public DbSet<User> Users { get; set; }
public DbSet<BusinessObjects> BusinessObjects { get; set; }
public DbSet<Property> Properties { get; set; }

}

}

The above would be your data context file that uses the DBContext file  in your models folder, just fyi (that is the common place for Entity Framework models in the standard  Visual Studio 2010 web solution structure).

Note, everything happens in the constructor.  This give you many interesting opportunities to programatically swap out Sql Server database names and users, compared to the web.config option, which tends to be far more static.  It allows far more extensive use of business objects and


24
Apr 12


Written By Steve French

 

Startups For the Rest of Us has the greatest quote ever on entrepreneurship

Via Rob Walling, the great quote ever is

Employees complain, entreupreneurs get it done.

Exactly what I needed to hear today.    The insight into a base mindset is what I like about it I suppose.


24
Apr 12


Written By Steve French

 

How to make a random name generator in C#

I recnetly came up with a simple random name generator to do an initial seed of a database for testing using real names,a nd I thought I would share. Here is my NameGenerator.cs file. It’s fairly simple, it just picks a name at random out of the top 100 first names and the top 100 last names for the year 2011. Enjoy!

The code for the random name generator in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNamespace
{
public class NameGenerator
{
public static Random rnd = new Random();

 

public static string GenRandomLastName()
{
List<string> lst = new List<string>();
string str = string.Empty;
lst.Add(“Smith”);
lst.Add(“Johnson”);
lst.Add(“Williams”);
lst.Add(“Jones”);
lst.Add(“Brown”);
lst.Add(“Davis”);
lst.Add(“Miller”);
lst.Add(“Wilson”);
lst.Add(“Moore”);
lst.Add(“Taylor”);
lst.Add(“Anderson”);
lst.Add(“Thomas”);
lst.Add(“Jackson”);
lst.Add(“White”);
lst.Add(“Harris”);
lst.Add(“Martin”);
lst.Add(“Thompson”);
lst.Add(“Garcia”);
lst.Add(“Martinez”);
lst.Add(“Robinson”);
lst.Add(“Clark”);
lst.Add(“Rodriguez”);
lst.Add(“Lewis”);
lst.Add(“Lee”);
lst.Add(“Walker”);
lst.Add(“Hall”);
lst.Add(“Allen”);
lst.Add(“Young”);
lst.Add(“Hernandez”);
lst.Add(“King”);
lst.Add(“Wright”);
lst.Add(“Lopez”);
lst.Add(“Hill”);
lst.Add(“Scott”);
lst.Add(“Green”);
lst.Add(“Adams”);
lst.Add(“Baker”);
lst.Add(“Gonzalez”);
lst.Add(“Nelson”);
lst.Add(“Carter”);
lst.Add(“Mitchell”);
lst.Add(“Perez”);
lst.Add(“Roberts”);
lst.Add(“Turner”);
lst.Add(“Phillips”);
lst.Add(“Campbell”);
lst.Add(“Parker”);
lst.Add(“Evans”);
lst.Add(“Edwards”);
lst.Add(“Collins”);
lst.Add(“Stewart”);
lst.Add(“Sanchez”);
lst.Add(“Morris”);
lst.Add(“Rogers”);
lst.Add(“Reed”);
lst.Add(“Cook”);
lst.Add(“Morgan”);
lst.Add(“Bell”);
lst.Add(“Murphy”);
lst.Add(“Bailey”);
lst.Add(“Rivera”);
lst.Add(“Cooper”);
lst.Add(“Richardson”);
lst.Add(“Cox”);
lst.Add(“Howard”);
lst.Add(“Ward”);
lst.Add(“Torres”);
lst.Add(“Peterson”);
lst.Add(“Gray”);
lst.Add(“Ramirez”);
lst.Add(“James”);
lst.Add(“Watson”);
lst.Add(“Brooks”);
lst.Add(“Kelly”);
lst.Add(“Sanders”);
lst.Add(“Price”);
lst.Add(“Bennett”);
lst.Add(“Wood”);
lst.Add(“Barnes”);
lst.Add(“Ross”);
lst.Add(“Henderson”);
lst.Add(“Coleman”);
lst.Add(“Jenkins”);
lst.Add(“Perry”);
lst.Add(“Powell”);
lst.Add(“Long”);
lst.Add(“Patterson”);
lst.Add(“Hughes”);
lst.Add(“Flores”);
lst.Add(“Washington”);
lst.Add(“Butler”);
lst.Add(“Simmons”);
lst.Add(“Foster”);
lst.Add(“Gonzales”);
lst.Add(“Bryant”);
lst.Add(“Alexander”);
lst.Add(“Russell”);
lst.Add(“Griffin”);
lst.Add(“Diaz”);
lst.Add(“Hayes”);

str = lst.OrderBy(xx => rnd.Next()).First();
return str;
}
public static string GenRandomFirstName()
{
List<string> lst = new List<string>();
string str = string.Empty;
lst.Add(“Aiden”);
lst.Add(“Jackson”);
lst.Add(“Mason”);
lst.Add(“Liam”);
lst.Add(“Jacob”);
lst.Add(“Jayden”);
lst.Add(“Ethan”);
lst.Add(“Noah”);
lst.Add(“Lucas”);
lst.Add(“Logan”);
lst.Add(“Caleb”);
lst.Add(“Caden”);
lst.Add(“Jack”);
lst.Add(“Ryan”);
lst.Add(“Connor”);
lst.Add(“Michael”);
lst.Add(“Elijah”);
lst.Add(“Brayden”);
lst.Add(“Benjamin”);
lst.Add(“Nicholas”);
lst.Add(“Alexander”);
lst.Add(“William”);
lst.Add(“Matthew”);
lst.Add(“James”);
lst.Add(“Landon”);
lst.Add(“Nathan”);
lst.Add(“Dylan”);
lst.Add(“Evan”);
lst.Add(“Luke”);
lst.Add(“Andrew”);
lst.Add(“Gabriel”);
lst.Add(“Gavin”);
lst.Add(“Joshua”);
lst.Add(“Owen”);
lst.Add(“Daniel”);
lst.Add(“Carter”);
lst.Add(“Tyler”);
lst.Add(“Cameron”);
lst.Add(“Christian”);
lst.Add(“Wyatt”);
lst.Add(“Henry”);
lst.Add(“Eli”);
lst.Add(“Joseph”);
lst.Add(“Max”);
lst.Add(“Isaac”);
lst.Add(“Samuel”);
lst.Add(“Anthony”);
lst.Add(“Grayson”);
lst.Add(“Zachary”);
lst.Add(“David”);
lst.Add(“Christopher”);
lst.Add(“John”);
lst.Add(“Isaiah”);
lst.Add(“Levi”);
lst.Add(“Jonathan”);
lst.Add(“Oliver”);
lst.Add(“Chase”);
lst.Add(“Cooper”);
lst.Add(“Tristan”);
lst.Add(“Colton”);
lst.Add(“Austin”);
lst.Add(“Colin”);
lst.Add(“Charlie”);
lst.Add(“Dominic”);
lst.Add(“Parker”);
lst.Add(“Hunter”);
lst.Add(“Thomas”);
lst.Add(“Alex”);
lst.Add(“Ian”);
lst.Add(“Jordan”);
lst.Add(“Cole”);
lst.Add(“Julian”);
lst.Add(“Aaron”);
lst.Add(“Carson”);
lst.Add(“Miles”);
lst.Add(“Blake”);
lst.Add(“Brody”);
lst.Add(“Adam”);
lst.Add(“Sebastian”);
lst.Add(“Adrian”);
lst.Add(“Nolan”);
lst.Add(“Sean”);
lst.Add(“Riley”);
lst.Add(“Bentley”);
lst.Add(“Xavier”);
lst.Add(“Hayden”);
lst.Add(“Jeremiah”);
lst.Add(“Jason”);
lst.Add(“Jake”);
lst.Add(“Asher”);
lst.Add(“Micah”);
lst.Add(“Jace”);
lst.Add(“Brandon”);
lst.Add(“Josiah”);
lst.Add(“Hudson”);
lst.Add(“Nathaniel”);
lst.Add(“Bryson”);
lst.Add(“Ryder”);
lst.Add(“Justin”);
lst.Add(“Bryce”);

//—————female

lst.Add(“Sophia”);
lst.Add(“Emma”);
lst.Add(“Isabella”);
lst.Add(“Olivia”);
lst.Add(“Ava”);
lst.Add(“Lily”);
lst.Add(“Chloe”);
lst.Add(“Madison”);
lst.Add(“Emily”);
lst.Add(“Abigail”);
lst.Add(“Addison”);
lst.Add(“Mia”);
lst.Add(“Madelyn”);
lst.Add(“Ella”);
lst.Add(“Hailey”);
lst.Add(“Kaylee”);
lst.Add(“Avery”);
lst.Add(“Kaitlyn”);
lst.Add(“Riley”);
lst.Add(“Aubrey”);
lst.Add(“Brooklyn”);
lst.Add(“Peyton”);
lst.Add(“Layla”);
lst.Add(“Hannah”);
lst.Add(“Charlotte”);
lst.Add(“Bella”);
lst.Add(“Natalie”);
lst.Add(“Sarah”);
lst.Add(“Grace”);
lst.Add(“Amelia”);
lst.Add(“Kylie”);
lst.Add(“Arianna”);
lst.Add(“Anna”);
lst.Add(“Elizabeth”);
lst.Add(“Sophie”);
lst.Add(“Claire”);
lst.Add(“Lila”);
lst.Add(“Aaliyah”);
lst.Add(“Gabriella”);
lst.Add(“Elise”);
lst.Add(“Lillian”);
lst.Add(“Samantha”);
lst.Add(“Makayla”);
lst.Add(“Audrey”);
lst.Add(“Alyssa”);
lst.Add(“Ellie”);
lst.Add(“Alexis”);
lst.Add(“Isabelle”);
lst.Add(“Savannah”);
lst.Add(“Evelyn”);
lst.Add(“Leah”);
lst.Add(“Keira”);
lst.Add(“Allison”);
lst.Add(“Maya”);
lst.Add(“Lucy”);
lst.Add(“Sydney”);
lst.Add(“Taylor”);
lst.Add(“Molly”);
lst.Add(“Lauren”);
lst.Add(“Harper”);
lst.Add(“Scarlett”);
lst.Add(“Brianna”);
lst.Add(“Victoria”);
lst.Add(“Liliana”);
lst.Add(“Aria”);
lst.Add(“Kayla”);
lst.Add(“Annabelle”);
lst.Add(“Gianna”);
lst.Add(“Kennedy”);
lst.Add(“Stella”);
lst.Add(“Reagan”);
lst.Add(“Julia”);
lst.Add(“Bailey”);
lst.Add(“Alexandra”);
lst.Add(“Jordyn”);
lst.Add(“Nora”);
lst.Add(“Carolin”);
lst.Add(“Mackenzie”);
lst.Add(“Jasmine”);
lst.Add(“Jocelyn”);
lst.Add(“Kendall”);
lst.Add(“Morgan”);
lst.Add(“Nevaeh”);
lst.Add(“Maria”);
lst.Add(“Eva”);
lst.Add(“Juliana”);
lst.Add(“Abby”);
lst.Add(“Alexa”);
lst.Add(“Summer”);
lst.Add(“Brooke”);
lst.Add(“Penelope”);
lst.Add(“Violet”);
lst.Add(“Kate”);
lst.Add(“Hadley”);
lst.Add(“Ashlyn”);
lst.Add(“Sadie”);
lst.Add(“Paige”);
lst.Add(“Katherine”);
lst.Add(“Sienna”);
lst.Add(“Piper”);

str = lst.OrderBy(xx => rnd.Next()).First();
return str;
}
}
}


23
Apr 12


Written By Steve French

 

Less Accounting Review – Part 1, My Accounting Background

'Accounting Sense != Common Sense' photo (c) 2007, Paul Downey - license: http://creativecommons.org/licenses/by/2.0/I recently signed up with Less Accounting and I will be publishing a Less Accounting review  and experiences with the setup process here (read the introduction).

  1. Introduction
  2. My accounting background (This post)
  3. The Signup
  4. Import Quickbooks File
  5. Wire Account
  6. Set Recurring
  7. Hook up Bank Account
  8. Hook Up American Express Account
  9. Less Accounting – One Week Later
  10. Less Accounting – One Month Later

In the beginning, by which I mean my awesome web company’s beginning in 2002, there was Microsoft Money.  That was my first accounting software and it served me adequately for about five years.  It was nothing special, but it did the job with a moderate amount of hassle, but after a while it began to show it’s age.   Somewhere along the way I started using BlinkSale, which was one of my better decisions.

Along about this time I first heard about Microsoft Office Accounting.  It was a relatively easy migration over from Microsoft Money and I was able to do my minimal accounting and bookkeeping tasks in significantly less time.  Plus, it worked like every other Microsoft product, and if you know Microsoft products well (which I do) then using it was a breeze.  Then Microsoft decided to discontinue the program and for reasons I don’t recall I thought that was a big deal and did not want to get stuck using obsolete software.

So I got a copy of QuickBooks, and away I went.  Specifically I went through a painful migration software.  I also came to the realization that there were many parts of QuickBooks that were not compatible with 64 bit software at that time.  I debated using Quickbooks Online, but that did not seem like a good option.

The good part of using QuickBooks for my Accounting Software

My accountant knew and liked the system, and we could do the entire thing remotely.

Beyond that, there were no good parts.  After a while, and inspired by Peter Drucker’s book, The Effective Executive, I began looking for options.  That led me to Less Accounting for my financial management and financial analysis..


11
Apr 12


Written By Steve French

 

How to use UI Hints and Display Templates in ASP.net MVC 3

'wtf - code quality measurement' photo (c) 2008, smitty42 - license: http://creativecommons.org/licenses/by-nd/2.0/Surprisingly there is very little information about UI Hints and Display templates for ASP.net MVC 3, so I thought I would share what I have learned.

What are Display Templates?

Display Templates are lovely features of ASP.net MVC 3 that allow you to have a common formatting for certain properties of your objects.  For example, take the belowp roperty (which should be located in your models folder)

[DataType(DataType.ImageUrl)]
[UIHint(“ImageUrl”)]
[Display(Name = “Photo”)]
public string Photo { get; set; }

Everything looks like a standard property, UIHint property.  It would correspond to the Display Template (which is below, it would be name “ImageUrl.cshtml” and would be located in your Views/Shared/DisplayTemplates folder)

@model string
<img style=”display: block;” src=”@Model” alt=”” align=”right” />

What does the UI Hint do?

The UI Hint tells ASP.net MVC 3 to always display the Photo property using the formatting in the ImageUrl.cshtml file.  That way everything is nice, clean and using much less code.

How do you call UI Hints and Display Templates?

Just use the Html.DisplayFor command, for example the Display template below would look like this in Visual Studio 2010

@Html.DisplayFor(xx => xx.Photo)

and would render as

<img style=”display: block;” src=”/img/Photos/Photo123.png” alt=”” align=”right” />

in the browser, allowing you to keep formatting and style consistent across your entire site with very little effort.

 


10
Apr 12


Written By Steve French

 

It’s FaceBook vs Google Plus – I make them race

I’ve started an open competition between my Facebook people and My Google+ people for interesting responses

The Challenge

What is the weirdest thing you think is true, or at least likely? I’ll see if anyone responds and after a week I’ll declare a winner.

Just to get started, here are a few of mine.

  1. Once the generations that grew up with lead filled gas dies off most of America’s social problems will disappear.
  2. Humanity is putting massive amounts of carbon in the atmosphere, causing climates to change, but the end result will not be global warming. Freak snowstorms or flying scorpions, but not global warming

The winner will get much more of my attention when it comes to social networking.


03
Apr 12


Written By Steve French

 

How to fix the Initialization method TestInitialize threw exception problem in In Visual Studio 2010 Unit Testing

The Problem

You are making an ASP.net MVC 3 web application, and being a good person, you are unit testing as much as possible.  You have properly separated your database from your business objects and every other good practice.  However, when you unit test your controllers, you throw in a fake DBContext (I am assuming you are using the Entity Framework and Moq) and all of a sudden, you get the following error

<b>Failed 1 tests.</b>
Initialization method [YourTestClass].TestInitialize threw exception. System.ArgumentNullException: System.ArgumentNullException: Value cannot be null.
Parameter name: source.

Which certainly makes for one of the least helpful error messages of all time.

The Cause

The problem seems to be lazy loading and the DBSet Feature of Entity Framework.  For example the below passage in your source code below will throw the error

public virtual DbSet<Setting> Settings { get; set; }
public virtual DbSet<AccountSource> AccountSources { get; set; }

This bit of code will not thow the error

public DbSet<Setting> Settings { get; set; }
public DbSet<AccountSource> AccountSources { get; set; }

The problem seems to be entirely in the initialization method.

The Solution

Rather irritating isn’t it?  Especially since Lazy Loading is the design pattern to use, and works great in every other case.  However, if you just delete the “virtual” keyword everything works as intended.

 


03
Apr 12


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.