Get Adobe Flash playerPlugin by wpburn.com wordpress themes

This is something I’ve been thinking about a lot. When do I throw an exception? Do I program with exceptions? Do I catch those exceptions.

Consider this piece of code:

public Product GetProduct(int id)
{
    //get the product, or null if not found
    Product p = //...

    return p;
}

Now you can ask yourself the following question:

Is it ok to return null if the product is not found? After all, the calling layer assumes that we’ll be getting a Product, not a null.

So now the calling layer needs to check if the product != null.

It would be exceptional if no product was found.

So in my opinion you throw an exception if no product is found. And that get’s handled in the calling layer.

But on the other hand: if you would just return a null and test on that in the calling layer you would use less resources since exception throwing is expensive.

I’d still go with the first one since it goes better with my consume code thoughts.

But this is an agreement you need to make across your team, and across your API.

Comments No Comments »

So yesterday I reinstalled my laptop because some beta driver was acting up, no big deal. I had both of my partitions secured with Bitlocker (and a BIOS password set up) so that my laptop is secure.

After formatting I noticed that my C drive wasn’t encrypted anymore (which is obvious, it was formatted).

But my D drive looked like this:

locked

It was locked. Fortunately I printed my recovery key so I was able to unlock the drive.

Please print the keys, and keep them safe!

Comments No Comments »

Well not really my computer, but my girlfriend’s. It needed some updating (you know how people are, not updating & stuff).

Adobe Reader already sets 2 programs in startup. A speed launcher and another one which I am too lazy to identify. If your application is too slow then optimize it, don’t treat the symptoms.

Then our good slow friend Java. Always had their speed launcher (symptom treatment!!) in startup, but guess what also added a service.

naamloos

What the f*ck? I’m moving closer and closer to not install Java on new pcs, since it’s a burdon to manage and keep up to date!

Comments No Comments »

Something not many people know: when you need to compare 2 strings in C#.NET you can use the == operator overload:

if(firstString == secondString)
{
    //...
}

This is case sensitive. But when you need to ignore the case please don’t use this:

if(firstString.ToLower() == secondString.ToLower())
{
    //...
}

or this:

if(firstString.ToLower().Equals(secondString.ToLower()))
{
    //...
}

A Framework provides you with utilities, please use them. They are usually better than your implementation!

Use this:

if (firstString.Equals(secondString, StringComparison.OrdinalIgnoreCase))
{
    //...
}

StringComparison is an enum. So please look at the options :)

Comments No Comments »

Ever wanted a getJSON which does a post?

Here it is:

jQuery.extend({
    postJSON: function(url, data, callback)
    {
        $.post(url, data, callback, "json");
    }
});

Comments No Comments »

To connect to your SQL Server 2008 Express edition over network you need to enable the following options:

First: open SQL Configuration Manager (SQLServerManager10.msc):

image

Double click on ‘Named Pipes’ and enable them.

image

Then click ‘OK’ on the bottom. Now we have the access set up, now we need to start the service.

Now open Services (services.msc) and double click on ‘SQL Server Browser’:

image

Set the startup type to ‘Manual’ or ‘Auto’. That’s up to you. I have it on manual for security reasons. Then you can click start. But we have one more step to do. In the window copy the path to the executable (’C:\Program Files (x86)\Microsoft SQL Server\90\Shared\sqlbrowser.exe’).

Then go to your firewall in your control panel and add the sqlbrowser path to allowed programs.

You’ll eventually see this:

image

You can also check the public, but that’s not necessary for me. If you set the sqlbrowser service to manual, you’ll need to start it each time you want it (just by making a shortcut to the path we copied above.) If not, it’ll start at startup :)

Good luck and post your questions below!

I’m going to try replication between 2 SQL Servers Express in one of these days and I’ll post the results on that too!

Comments No Comments »

Today I was trying to find out a way to use jQuery in one of my views. Since I include the jQuery in my master page I bumped into the problem that Visual Studio doesn’t recognize the jQuery in the child page.

So I tried some stuff, and I thought: I might share it as well here so you don’t spend time on looking up everything like I did.

First thing I tried is using the ///<reference path … /> syntax.

But this doesn’t work in <script …></script> tags. It only works in real (.js) javascript files.

I then used this ‘hack’ to make the intellisense work (and I don’t like hacks).

    <% if (false)
    { %>
    <script type="text/javascript" src="~/Scripts/jquery-1.3.2-vsdoc.js"></script>
    <% } %>

Stuff like this really makes my code look like a hack. I’ll test tonight if I have different behavior in Visual Studio 2010.

Comments No Comments »

If you have a Ajax.Actionlink and you want to specify a OnBegin with a parameter you need to use the following syntax:

<%=Ajax.ActionLink("text", "Action", "Controller", new { param = someVar }, new AjaxOptions() { OnBegin = string.Format("new Function(‘DoSomeThing(this)’)", index),  HttpMethod = "POST" })%>


Where DoSomeThing is just a function that takes a string as parameter.

Update: added quotes around the real (inner) function, otherwise it’s executed instead of passed on at the creation of the object.

Comments No Comments »

(scroll down for an in depth explanation)

I’m writing this post since I had a lot of problems finding out how Model binding works in ASP.NET MVC.

This behavior has changed in the beta and the final. This causes that some blog posts on the internet are inaccurate.

But let’s set things straight.

Create a MVC Project, leave everything default and add Product in your Models folders with the following (very simple) code:

namespace BlogPost.Models
{
    public class Product
    {
        public string Name { get; set; }
    }
}

Then add a ProductsController (in the Controllers folder) with the following code:

using System.Collections.Generic;
using System.Web.Mvc;
using BlogPost.Models;

namespace BlogPost.Controllers
{
    public class ProductsController : Controller
    {
        public ActionResult Add()
        {
            return this.View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(IList<Product> products)
        {
            return this.View();
        }
    }
}

And add the following view for the Add function:

image

With the following markup:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Add up to 10 products to the database
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Add up to 10 products to the database</h2>
    <%
        using (Html.BeginForm())
        {
            for(int x = 0;x <= 9;x++)
            {
                %>
                <fieldset>
                    <legend>Product <%=x %>:</legend>
                    <input type="text" name="products[<%=x %>].Name" />
                </fieldset>
                <%
            }

            %><input type="submit" name="submitForm" value="Save products" /><%
        }
    %>
</asp:Content>

Now put a breakpoint in the Add function in your ProductsController that handles the Http Post.

image

Hit f5 and go to <your url>/Products/Add:

image

Enter 10 names, and hit the submit button at the bottom.

Visual Studio will break on your this.View(). Hover over your products parameter and you’ll see something in the lines of this:

image

Not too hard is it?

A little bit more in depth:

ASP.NET MVC binds your HTML input to your parameter through the default model binder. I recommend you read the source code available on http://aspnet.codeplex.com/SourceControl/changeset/view/23011

image

If you want to step through the source code using your application I refer you to Steve Sanderson’s excellent blog post.

If you take a look at the generated HTML source code you’ll notice this:

<fieldset>
    <legend>Product number 0:</legend>
    <input type="text" name="products[0].Name" />
</fieldset>
<fieldset>
    <legend>Product number 1:</legend>
    <input type="text" name="products[1].Name" />
</fieldset>

The name ‘products’ is VERY important, since this specifies that it your input should be bound to the ‘products’ parameter in your controller action.

Then the index between brackets: IT MUST START FROM 0 AND BE CONTINUES!

Then the .Name, which specifies that your input should be placed in the Name property (not just a public variable, but a property with a get/set) (see code above).

You can repeat this for multiple properties, and group them by product.

That’s it for simple model binding.

Hope it helps.

Comments No Comments »

When you right click a music folder in Windows Explorer you get this:

 

Shop for music online

To remove the Shop for music online link put this into a registry file and execute it:

Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\SystemFileAssociations\Directory.Audio\shellex\ContextMenuHandlers\WMPShopMusic]

Comments No Comments »

And shepherds we shall be, for thee my Lord for thee, power hath descended forth from thy hand, that our feet may swiftly carry out thy command. We shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine Patris, et Filii, et Spiritus Sancti.