Macro to insert date time in Visual Studio 2010

My current assignment at work has me working through a lot of old code. Code that now needs to be modified to implement new features and remove bugs. While updating or modifying existing code it is very important to insert comments stating why the modifications were carried out and the date on which they were carried out. Eventually when other developers work on the same piece of software and compare revisions, they shouldn’t be left wondering why a certain modification was made.

Unfortunately, Visual Studio lacks insert date time at current position functionality. But what it does support are Macros, and using those, it’s quite easy to implement that feature ourselves.

Macros are written in VB.NET. The following VB.NET code can be used to insert the date time at the current caret position –

Sub PrintDateTime()   
    If (Not IsNothing(DTE.ActiveDocument)) Then   
        Dim selection As TextSelection = DTE.ActiveDocument.Selection   

The code above creates a module called PrintDateTime.

  1. It first checks if there is an active document, if there, it gets the currently selected text.
  2. It then inserts current date time at the currently selected caret location.

Next step is to place this code in the correct place.

Macros in Visual Studio 2010
  1. Open up the Macro explorer by pressing ALT + F8.
  2. In the Macro explorer right click on Macros, and click on ‘New Macro Project’.
  3. Give it a name, and once it’s created, it should be visible in the Macro explorer.
  4. Create a new Module under the project and give it a name that relates to what you are doing
  5. Double click on the new Module to open a Macro editor.
  6. Paste the code above in that editor and save the file.

Now that that is done, your Module should be visible under your project. The next thing we have to do is to assign our newly created Macro to a shortcut key, so that we can easily run it whenever we want.

  1. On the file menu, click on Tools > Options
  2. On the window that opens up, in the tree view, go to Environment > Keyboard
  3. In the Show commands containing textbox, type the name of the Module you created earlier and it should show up in the list below the textbox. Select it.
  4. In the Press shortcut keys textbox, press the combination of keys that you want to use whenever you wish to run your Macro.
  5. Make sure Global is selected under Use new shortcut in: and then press the Assign button, followed by OK.

It’s time to see if what we did works! Open up a document and press the same combination of keys that you used to create the shortcut. Once you do, the date time should be inserted at your current caret position and this should work for any document that you create inside Visual Studio 2010.

Passing JavaScript object to ASP.NET web service or method

I recently came across a question on StackOverflow, where the author was having trouble calling a JSON based web service from his ASPX page using jQuery. He was passing a user defined JavaScript object. I decided that I would quickly put a project together describing how to do so, for my future reference, and for anyone else who needs help. Here is my original answer on StackOverflow. I’ll be offering a higher level of explanation here in the post.

Below is the web method that we’ll be calling, in my ASPX page code behind. Note that a web method has to be declared as static,

public static Contact getContact(Contact cnt)
{"Abijeet Patro";"Blah Blah";
    return cnt;

The user defined object that you pass through JavaScript has to have the same properties as the user defined object on the server. Say for example that you were passing the following from JavaScript –

var cnt = { name : 'Hello', phone :'Hello' };

The object we defined in JavaScript has two properties, name and phone in lower case letters.

This is exactly similar to the class defined on the server

public class Contact 
    public string name {get;set;}
    public string phone {get;set;}

Note that the properties in the class have to be public

Now we’ll see how to call this web method from our ASPX page,

$(document).ready(function () {
    var cnt = {name:'Hello',phone:'Hello'};
        type: "POST",
        url: "/Default.aspx/getContact",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify({'cnt':cnt}), // Check this call.
        success: function (data) {

For an explanation of the options used here check jQuery ajax API documentation,

I’ll be discussing two noteworthy things,

1. Sending data to the server

Data (data) contains the object we will be sending to the server. Before we send the object to the web method, we have to ensure that we convert the object into a JSON format. We can do this by calling JSON.stringify. This javascript method is supported in all modern browsers*. The values that you pass to JSON.stringify have to be similar to what I have specified here. The name of the object we are passing to the server is named as 'cnt' in single or double quotes. This also has to be exactly same as the variable name being accepted by the web method.

public static Contact getContact(Contact cnt)

2. Specifying what to connect to

The url specifies the web method or web service that we wish to call. If we were calling an ASP.NET based web service, we’d just have to change the url to /Default.asmx/getContact. Note that the name of the method that we wish to call is concatenated to the end of the web service path.

Now let’s see everything in action using Fiddler, a nifty network monitoring tool.

You’ll notice the call, with the request data sent to the server, and the response that is received from the server. If the call succeeds the success function of jQuery ajax is called. The server returns data as a JSON object and we can access it using the d property of the object. ASP.NET adds the property by default as of .NET Framework 3.5 to protect you from JSON hijacking.

Data sent to the server

Response from the server

So that’s basically it, if you have any doubts, ping me through the comment section. You’ll find the source code for the application here.

JSON.stringify is not supported by Internet Explorer 7 by default. To add this function to it, add JSON2.js to your page.