h1

Silent install of SQL Server 2008

September 23, 2008

Are you having trouble installing SQL Server 2008 from the command prompt? I tried installing SQL Server Express 2008 by looking at the reference on MSDN. The trouble was it kept failing time and again and the screen disappeared faster than you can see what the error message was. Fortunately, the installer creates a Summary.txt file in the folder C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log. Opening the file will tell you exactly what the error message is.

But sometimes even this isn’t enough. In my case,  I was quickly able to find that I needed PowerShell for installing SQL Server Express with Advanced Services – which was quickly fixed, but it kept failing with this exception -

Exception type: Microsoft.SqlServer.Setup.Chainer.Workflow.NoopWorkflowException
Message:
No features were installed during the setup execution.The requested features may already be installed. Please review the summary.txt log for further details.

Our friend Google wasn’t of much help, so I decided to do a non-command line install to see if it was installing in the machine. It turns out, once you manually set all the configuration information, the installer creates a file called ConfigurationFile.ini and puts those settings there. This shows up in the Ready to Install step  -

image

The installer then uses this file to do the actual install. Now, if you open the configuration file, you will find that these are the same settings you can set from the Command line. It looks something like this -

;SQLSERVER2008 Configuration File
[SQLSERVER2008]

; Specify the Instance ID for the SQL Server features ...

INSTANCEID="SQLExpress"

; Specifies a Setup work flow, like INSTALL, UNINSTALL, ...

ACTION="Install"

; Specifies features to install, uninstall, or upgrade ...
FEATURES=SQLENGINE,RS

...
...

You can then add all the name value pairs together to get the right command line arguments. Like so –

SQLEXPRADV_x64_ENU.exe /QUIET /INSTANCEID="SQLExpress"
/ACTION="Install" /FEATURES=SQLENGINE,RS ...

There you have it – the right command line parameters to install SQL Server 2008 Express with Advanced Services silently from the command prompt. You can choose to remove some of the parameters that have default values or just leave them as they are – it doesn’t really matter.

h1

Visual Studio Tips, Tricks and Techniques Talk

September 22, 2008

I will be presenting on Visual Studio Tips, Tricks and Techniques at the Victoria .NET Dev SIG tomorrow. The meeting is at Innovation@257 on Collins Street in the City. If you can’t make it to the meeting, you can also view it online by registering here:

My co-worker Jordan Knight will be presenting on ASP.NET AJAX History in the same session.

h1

Twitter account for Vic .NET Dev SIG

September 22, 2008

I’ve created a Twitter Account for broadcasting event details of Victoria .NET Dev SIG. You can follow them at http://www.twitter.com/vdnug

You can also follow me on http://www.twitter.com/MaheshKrishnan

h1

Visual Studio Debugging Tips and Tricks

September 9, 2008

In the “Hour of Power” presentation at Tech.Ed, I showed how Visual Studio 2008 could be used for a better Debugging Experience. I tried to showcase 10 tips and mentioned that I’ll update the details of it in my blog. Here it is-

1. Debugging/Stepping into Framework code

With the launch of Visual Studio 2008, Microsoft have allowed developers to debug into the .NET Framework code.  This doesn’t happen by default however, and some settings need to be configured. The steps involved in configuring this for Visual Studio 2008 SP1 are:

  • Go to Tools->Options menu item and select Debugging/General on the left side of the dialog
  • Ensure that Enable .NET Framework source stepping option is checked
  • Ensure that Enable source server support option is checked
  • Select Debugging/Symbols on the left side of the dialog
  • Add the Symbol file location http://referencesource.microsoft.com/ symbols
  • Specify a cache symbol location directory

image

image

Once this is set, you will be able to step into .NET Framework code, set break points in it and look at values of private data members.  This not only provides the ability to debug the .NET Framework source code, it also provides a great reference implementation of classes.

2. Moving the Execution Point

When you are stepping through code in Visual Studio, the IDE displays the yellow arrow on the line of the code that it is about to execute.

image

This yellow arrow can be dragged and dropped on to any line that needs to be the execution line. Although, dragging the code forward can have some undesirable consequences if variable declaration and assignments are skipped, this feature can be very useful if used properly.

3. Step into Specific

In older Visual Studio versions, while stepping into code that has more than one function in the same line, the debugger ends up stepping into and out of each function. This can be very annoying at times. Visual Studio 2008 provides a functionality called Step Into Specific. This can be accessed from the context menu while debugging and allows you to step specifically into the function that you are interested in.

image

4. Step over properties and operators

Another annoying thing in older versions of Visual Studio was that the debugger would step into properties and operators. In a lot of cases, the properties were just wrappers for private data members, and stepping in and out of these properties was not particularly useful. One way to get around this problem is to decorate the property with the attribute DebuggerStepThrough. This would prevent the debugger from stepping into the property.

Visual Studio 2008 provides a much better debugging experience by providing an option called Step over properties and operators, that allows you to chose whether you would like to step into the property’s source code or not. This option can be set from the Options dialog -

image

It is also available in the context menu that pops up while debugging -

image 

5. Tracepoints

Visual Studio 2008 allows developers to easily add Tracepoints to look at values while debugging. This feature was also available in older versions of Visual Studio, but accessing this has just become a whole lot easier. So, what are Tracepoints? They are breakpoints, that allow you to execute something like printing a trace message on the Debug output window and optionally continue the execution without stopping when the break point has been hit. Trace points can be enabled from the debug context popup menu as shown below:

image

The variables whose values are to be displayed in the output window will have to be enclosed in curly brackets as shown in the screen shot below.

image

6. Conditional break points and trace points

Visual Studio also allows you to add conditional break points and trace points that are hit only when a certain condition is met. This can be easily accessed by right-clicking on a break point glyph

image

As shown in the menu options, you can specify the condition, hit count or even a filter for the break (or trace) point.

image 

7. The DebuggerDisplay attribute

When inspecting values in the debugger, Visual Studio does a great job of displaying the values just by hovering over a variable. But when you are inspecting objects, it is not always helpful as this example shows:

image

You will have to drill further into the object to see what the values are. To get over this problem the class Book can over ride the implementation of ToString. The Debugger will then display this value when it is inspected.

However, this is not the right way of doing it. The right way of doing it is to decorate the class with the DebuggerDisplay attribute as shown below -

[DebuggerDisplay("{ISBN}:{Title}")]
class Book
{
    public string ISBN { get; set; }

    public string Title
    {
        get { return title; }
        set { title = value; }
    }
    ...
}

Anything displayed within the curly brackets in the Attribute string will be evaluated – in this case the two properties ISBN and Title will be expanded as shown below -

image

This is definitely an improvement, but it still needs a bit of clean up as the two strings show quotes and null is being displayed when ISBN is not present. To accomplish this, the DebuggerDisplay attribute can be changed to -

[DebuggerDisplay("{ISBN == null ? \"\" : ISBN + \": \",nq} {Title.Substring(0, System.Math.Min(Title.Length, 33)) + (Title.Length > 33 ? \"…\" : \"\"), nq}")] 

class Book
{
   ...
}

The nq option within the curly brackets is for no quotes. In addition, expressions can also be evaluated within the braces. This is used to to display an empty string if ISBN is null and also to display elipsis when the length of the book title exceeds 33 characters. The net result is something like this –

image

8. The DebuggerBrowsable attribute

If we expand the first object in the array from the example above, we will notice that the Title is shown twice -

image

This happens because the lower case “title” is a private data member that the property “Title” uses. In effect, they are both the same, but if we have a class that has a lot of private data members that are wrapped by properties, a lot of redundant information will be displayed. To prevent certain data members to not be displayed, they will have to be decorated with a DebuggerBrowsable attribute with the DebuggerBrowsableState.Never set as shown below -

 9. The DebuggerTypeProxy attribute

When the variable values are displayed in the debugger, Visual Studio automatically evaluates all data members and properties and displays them on the screen. The DebeuggerTypeProxy attribute allows an alternate class to be used to determine which data members and properties are to be displayed. In the example shown below, the proxy class even calls a method, which is not evaluated by the debugger -

[DebuggerDisplay("{ISBN == null ? \"\" : ISBN + \": \",nq} {Title.Substring(0, System.Math.Min(Title.Length, 33)) + (Title.Length > 33 ? \"…\" : \"\"), nq}")]
[DebuggerTypeProxy(typeof(ProxyView))]
class Book
{

    ...
}
class ProxyView
{
    Book book;
    public ProxyView(Book book)
    {
        this.book = book;
    }

    public string ConverToXml
    {
        get
        {
	    //Exposing method as a property
            return book.ConvertToXml();
        }

    }

    public string Title
    {
        get
        {
            return book.Title;
        }
    }

    public string ISBN
    {
        get
        {
            return book.ISBN;
        }
    }
}

10. The System.Diagnositcs Namespace

The System.Diagnostics class also contains a number of classes that can be used for printing out trace or debug information. The class Debug is used while compiling and running the application in Debug mode and the output is displayed in the Output window in VS 2008. The syntax for the Trace class is very similar to that of the Debug class with the exception that it will still be available while compiling and running the application in Release mode.

In the sample code shown below, a ConsoleTraceListener is added to redirect the traces to print the message to the console, rather than the output window

        Trace.Listeners.Clear();
        Trace.Listeners.Add(new ConsoleTraceListener());

        Trace.WriteLine("&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<trace>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;");
        Debug.WriteLine("[My Debug message]");
h1

TechEd: Here I come

September 2, 2008

TechEd Oz 2008 is here and I am heading off to Sydney tonight. Apart from being a great event where you get to learn heaps of stuff, I also get to catch up most of the guys from Readify and other Techies I haven’t seen in a while.

This year, I will be doing a “Visual Studio Tips and Tricks” session and also co-presenting the session “The Hour of Power”. If you are in TechEd, come by and say Hello :)

h1

PhotoSynth and the Goddess of Wealth

August 28, 2008

PhotoSynth was released last week and I’ve been meaning to play with it, but just didn’t find the time to do it. So, finally today after my 1 year old went to bed, I thought I’d have a quick look at how to create one.

I just took a few photos of a sculpture of Lakshmithe Hindu Goddess of Wealth, that sits at the entrance of my house. I fired up PhotoSynth, added the photographs, hit the Synth button and then, that was it. Done. within a matter of minutes, I had created my first Synth. I couldn’t believe how simple it was.

Here is the link to it:

http://photosynth.net/view.aspx?cid=4fd0e29a-5495-4af8-8cf0-804e22da9415

The Synth I created isn’t that great, but I put it together in a matter of minutes…This really is great technology.

h1

Tatham’s Vic.NET talk

June 20, 2008

If any of you missed Tatham Oddie’s Architectural Considerations for ASP.NET MVC framework talk given at the Victoria .NET Dev SIG, you can watch it here:

Enjoy!

h1

Extension Methods

May 30, 2008

In my series on the new C# features, I had previously blogged about -

In this post, I will be talking about Extension methods. Extension methods allow new methods to be added to an existing class without having to actually extend the class using inheritance.

For instance, if you wanted to add a function called ReverseString to the String class, you can add it without creating a new class that inherits from String. Of course, String being a sealed class cannot be extended and adding a function to it is not possible via inheritance.

The syntax for doing this looks like this -

public static string ReverseString(this string str)
{
   //code to reverse the string
}

Notice that the function has to be static and it takes this followed by the type to which the extension method needs to be added to as the first parameter.

Intellisense

Once an extension method has been created, it shows up in Intellisense – which is quite handy.

image

Notice that the icon for an extension method is different to the usual ones.

Usage Rules

When using extension methods, there are few rules that need to be followed -

  • Extension methods have to be declared in a static function in a top level static class
  • The first parameter has to start with “this” and it cannot appear multiple times in the parameters
  • The keyword “this” must be followed by the type that needs to be extended. The type can be an interface, abstract class, a concrete class or even a sealed class.
  • You cannot use things like “ref” and “out” with “this”
  • You cannot extend Properties, Events or operators (something for the future version, perhaps :) ?)

Ambiguity

If you have the same extension method declared in two separate static classes, then the compiler will throw an error message saying that the call is ambiguous when the actual function is called in code. Unlike namespaces that help resolve classes that share the same name, there is no way to resolve function name clashes.

Extension methods that currently exist

.NET framework 3.5 comes loaded with a lot of extension methods. For instance, the IEnumerable interface has extension methods such as Aggregate, Average, Concat, Count, GroupBy, etc. A lot of these extension methods are essential for implementing LINQ (which will be covered in a future post).

More Examples

Extension methods can have additional parameters, as this example shows -

public static string ExtensionMethodWithParameter(
                      this string str,
                      string value)
{
    //Do nothing, it is just an example
    return str;
}

You can also add extension methods to arrays -

public static float MyAverage(this int[] intArray)
{
    float sum = 0;
    foreach (var element in intArray)
    {
        sum += element;
    }
    return sum / intArray.Length;
}

They can also be added to the base class of them all – “object”. For example, this function converts any object into an Xml representation, based on its properties -

//Simple, shallow implementation. Converts any object to Xml
public static string ToXml(this object o)
{
    Type myType = o.GetType();

    XmlDocument xmlDoc = new XmlDocument();
    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(
        "1.0", "utf-8", null);

    // Create the root element
    XmlElement rootNode = xmlDoc.CreateElement(
        XmlConvert.EncodeName(myType.Name));
    xmlDoc.InsertBefore(xmlDeclaration,
        xmlDoc.DocumentElement);
    xmlDoc.AppendChild(rootNode);

    var properties = myType.GetProperties();
    foreach (PropertyInfo prop in properties)
    {

        XmlElement childNode =
            xmlDoc.CreateElement(prop.Name);
        childNode.AppendChild(
            xmlDoc.CreateTextNode(
            prop.GetValue(o, null).ToString()));

        rootNode.AppendChild(childNode);
    }

    return xmlDoc.InnerXml;

}
h1

C# Features talk at Ballarat

May 29, 2008

I’ll be giving a talk on C# 3.0 at the Ballarat .NET SIG. The talk will start at 6:00pm on the 11th of June at the Commander Center in Ballarat. After my presentation, Tatham Oddie will be giving his talk on ASP.NET MVC pattern. Come along, have a few pizzas/drinks, kick back and enjoy the presentations :)

h1

Code Camp Oz Barbie

April 29, 2008

Here are some snaps from the barbecue that Readify hosted at Code camp Oz:

DSC00090DSC00091

DSC00097DSC00099 DSC00100 DSC00102 DSC00104 DSC00105 DSC00106 DSC00098