Thursday, May 21, 2009

Safer Deletes and Updates on Live DataBase

Its always scary working with live data I found that I feel more confident when I take the following measures.

Do a Select first to check the where clause:

SELECT * FROM Foo WHERE FooID = 1000

Wrap your changes in a Transaction and roll it back if you did not get the expected result.

Begin Transaction
  DELETE FROM Foo WHERE FooID = 1000
IF @@RowCount <> 1 BEGIN
  Rollback Transaction
END
ELSE
  Commit Transaction

 

#     Comments [0]  
kick it on DotNetKicks.com
 Saturday, April 11, 2009

Visual Studio Throws an Error When Trying to Connect/Open an .mdf File

Problem: You try to open a .mdf file in visual studio and you get one of the following errors.

Connections to SQL Server files (*.mdf) require SQL Server Express 2005 to function properly.  Please verify the installation of the component or download from the URL:  http://go.microsoft.com/fwlink/?LinkId=49251

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.

Solution: Open Server Explorer add Data Connection. Make sure the Data Source = Microsoft SQL Server Database File (SqlClient) and browse to the .mdf file located in your source directory(There maybe on located in the bin directory but this will get copied over by what is in src on a build). Click the Advance button. In order to work I needed to switch the Data Source to .\SQLEXPRESS2005 (This will be different for each computer).

Final connection string should look something like this:

Data Source=.\SQLEXPRESS2005;AttachDbFilename=|DataDirectory|\NerdDinner.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True

#     Comments [1]  
kick it on DotNetKicks.com

ASP.Net MVC And Ninject

Problem: You want to be able use the Ninject frame work with the new and shiny ASP.Net MVC framework.

Solution: Assembly: Ninject.Framework.MVC Contains an Abstract Base class you can Inherit from to do most of your heavy lifting.

 

Some Code

Global.asax.cs

public class MvcApplication : NinjectHttpApplication
    {

        private static IKernel _kernal;

        protected override void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = "" } // Parameter defaults
                );
        }

        protected override IKernel CreateKernel()
        {
            if (_kernal == null)
            {
                var modules = new IModule[]
                        {
                            new AutoControllerModule(Assembly.GetExecutingAssembly()),
                            new DemoModual()
                        };
                _kernal = new StandardKernel(modules);
            }
            return _kernal;
        }
    }
#     Comments [1]  
kick it on DotNetKicks.com
 Wednesday, February 18, 2009

Inversion of Control vs Dependency Injection vs Strategy Pattern

Warning this my understanding as of today, who knows what tomorrow will bring.

I often hear the terms Inversion of Control(IOC), Dependency Injection(DI) and the Strategy Pattern thrown around in the same context, this was getting really confusing for me. So, I spent a couple of hours trying to clarify the differences.

IOC vs DI: Basically DI is a type of IOC. IOC is a broad abstract principle that covers not only DI but Event-Driven programming and numerous others.

So What is DI? DI is the way you provide the implementation for a dependent service. There are a number of DI frameworks out there I have recently been playing with Ninject mostly because I like ninjas... and the flow syntax for wiring up the dependencies looks nice.

DI vs Strategy Pattern: For a while now I have been using the Strategy pattern thinking it was the same as DI. I know, "Gosh such an idiot :P", but DI assembles the appropriate algorithms for the calling class. Strategy is just one of the patterns that lends itself well to DI.

More Info:

Dependency injection

Inversion of control

Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler

Inversion of Control vs Strategy Pattern

IoC containers are not about a design pattern

Thank you, Eric Ridgeway for answering my 200 questions.

 

#     Comments [0]  
kick it on DotNetKicks.com
 Tuesday, February 17, 2009

Unhandled Error in Silverlight 2 Application Code: 2104

Problem: After deploying your silverlight application to your Web server you get the following error.

Error: Unhandled Error in Silverlight 2 Application
Code: 2104   
Category: InitializeError
Message: Could not download the Silverlight application. Check web server settings    
Source File: TestPage.html
Line: 53
 

Solution: The MIME types on your webserver are not setup correctly to host your silver light application. The MIME types needed are:

.xaml    application/xaml+xml
.xap    application/x-silverlight-app
.xbap    application/x-ms-xbap

Hack: If you are like me and have your silverlight hosted on a shared hosting enviroment like godaddy you cannot change your MIME types. Instead change the .xap to .zip you must also change the source paramater value inside your page to match.

<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">

<param name="source" value="Microsoft.Windows.Controls.Samples.zip"/>

<param name="onerror" value="onSilverlightError" />

<param name="background" value="white" />

<param name="minRuntimeVersion" value="2.0.31005.0" />

<param name="autoUpgrade" value="true" />

<a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">

<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>

</a>

</object>

#     Comments [2]  
kick it on DotNetKicks.com
 Thursday, February 05, 2009

Twitter

I just joined twitter I have played around with it before and never understood the appeal. Well I understand now you really need a good client app, I use TweetDeck. After that its all about the search, people use "hash codes" to assign categories to there tweets(cool kid lingo for messages on twitter). e.g. #Seattle doing a search on this hash forms a pseudo chat room then you just add #Seattle to any messages you want to show up. Its a lot of fun.

Scott has a good guid on twitter

My Twitter

Search on a topic:

Send tweets on this topic:

See whats going on:

#     Comments [4]  
kick it on DotNetKicks.com
 Wednesday, February 04, 2009

Generic List Operations

Problem: You need a quick and easy way to manipulate elements contained in a list.

Solution: Lambda Expression, these expressions are used in the same fashion you would use anonymous methods but are less wordy.

Anonymous methods:

employees.FindAll(delegate(employee e1) 
  { 
    return e1.Name == "Dave"; 
  })

Lambda Expression:

employees.FindAll(e1 => e1.Name == "Dave");
employees.Sort((e1,e2) => e1.LastName.CompareTo(e2.LastName));

For more complex expressions use the syntax like (params) => {statment}.

employees.Sort((e1, e2) =>
    {                                      
        if (e1.LastName.CompareTo(e2.LastName) == 0)
        {
          return e1.FirstName.CompareTo(e2.FirstName);
        }
        else
        {
          return e1.LastName.CompareTo(e2.LastName);
        }
    });
For more Info:
MSDN
#     Comments [0]  
kick it on DotNetKicks.com
 Thursday, June 12, 2008

Resharper Templates

I like and use Resharper because it is cheaper than CodeRush and I don't have to remember as many key commands most things are done with an alt + enter. It also works well with Unit Testing and for more yummy goodness with testing download JP's templates for Resharper.

livetemplates.xml (14.74 KB)

filetemplates.xml (3.79 KB)
#     Comments [1]  
kick it on DotNetKicks.com

Wildwood Campsites

Just finished with a client’s website www.wildwoodcampsites.com, I am pretty happy with it and so are they.

Few things that where fun was implementing a "Get Directions," Map with Microsoft Virtual Earth and a Silverlight picture viewer. I have worked with the Google maps api in the past it wasn't bad, but I hate the registration it makes it a pain to move and reuse. I found virtual Earth API far superior with great examples. I was able to get something cool up and working with little Java Script experience. The picture viewer I did not code from scratch, dnntemplet.com was offering it for free as a Beta version; although it didn't work I was able to fix it by digging around in the source code. I am really excited to see what the web will look like coming up with Flex.org and Silverlight 2.0

#     Comments [2]  
kick it on DotNetKicks.com