String comparison

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 🙂

SQL Server 2008 Express Edition: Access over network

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!

ASP.NET jQuery and master pages

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.

ASP.NET MVC AjaxOptions, OnBegin / Onsuccess / OnFailure

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({0})’)", 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.

jQuery submit button and button named submit

Suppose you have a button named "submit":

<form method="post" action="" id="myForm">
    <fieldset>
        <input type="text" name="myField" />
        <input type="submit" name="submit" value="submit form" />
    </fieldset>
</form>

And you want to submit this form with jQuery:

<script type="text/javascript">
    SomeFunction()
    {
        $("#myForm").submit();
    }
</script>

That won’t work since you have a button named ‘submit’ which then screws up the jQuery function. If you rename it to ‘submitForm’ or something it will work fine.

My First MVC Extension

public static string Submit(this System.Web.Mvc.HtmlHelper html, string name, string value)
{
    TagBuilder tagBuilder = new TagBuilder("input");
    tagBuilder.MergeAttribute("value", value);
    tagBuilder.MergeAttribute("name", name);
    tagBuilder.MergeAttribute("type", "submit");
    return tagBuilder.ToString(TagRenderMode.SelfClosing);
}

Easy submit button creation through

<%=Html.Submit("submit", "Your Text") %>

Cast vs ‘as’

What’s the difference?

object dinner = new Dinner();
Dinner myDinner2 = (Dinner) dinner;
Dinner myDinner = dinner as Dinner;

Well, the first cast (which is really a cast) throws an error if dinner is null.

The second one doesn’t, so you have to check that by yourself.

Also see the MSDN reference on the ‘as’ keyword.