mha.dk

Random thoughts of wisdom - the personal blog of Michael Holm Andersen

Visual Studio 2010 Pro Power Tools

Microsoft have released a set of extensions to Visual Studio Professional (and above) which improves developer productivity. I especially like the ‘Highlight Current Line’ feature which makes it easy to locate the line where the cursor is located, like this:

image

Another cool feature is the ability to color tabs according to their project, like this:

image

The extensions contains a lot of other nice stuff, check it out / download of from MSDN:

http://visualstudiogallery.msdn.microsoft.com/en-us/d0d33361-18e2-46c0-8ff2-4adea1e34fef

PageRank Status for Google Chrome

If you’re working with SEO and are using Chrome as (one of your) browsers, you may want to check out/download the “PageRank Status” extension, which places a small PageRank icon next to the address bar:

image

You can download it from the ChromeFans website:

http://www.chromefans.org/chrome-plugins/chrome-pagerank-status-extension.htm

ASPX Edit Add-In For Visual Studio 2010

Rowland O’Connor has taken the time to update the ‘ASPX Edit’ plug-in to work with Visual Studio 2010 – you can download it here:

http://www.rowlandoconnor.com/2010/04/16/aspx-edit-add-in-for-visual-studio-2010/

Usage / install info is found here:

http://ardentdev.com/aspxedithelper/

.. thanks to Jacob for the update :-)

AutoComplete, PageMethod and LINQtoSQL

I’m using AutoComplete to enhance the functionality of a TextBox and make it act like the below image, where the user enters the customer accountnumber, which is then displayed together with their name (the blurry stuff in the picture :-)

autocompletesearch

As I have to use this functionality on many pages, I needed it to be a UserControl.

I wanted to use a PageMethod (instead of a Web Service) to populate the AutoCompleteExtender, but since the page method must reside on the page (in the .aspx.cs file, it does not work if it’s placed in the .ascx.cs file – dammit MS), I wanted to limit the code needed on the .aspx.cs page to a bare minimum.

The number of entries displayed in the AutoComplete DropDown list had to be limited, but the user need to know how many matches the prefix entered.


The Page Method (ServiceMethod) of the AutoCompleteExtender requires a string[] to work and I wanted the Page Method to contain as little code as possible. I ended up with this:

[System.Web.Services.WebMethodAttribute(), 
System.Web.Script.Services.ScriptMethodAttribute()] public static string[] GetDataCustomerAccountNo(string prefixText,
int count, string contextKey) { CustomerRepository customerRepository = new CustomerRepository(); return customerRepository.GetCustomerAccountNumbersByPrefix(prefixText); }


The GetCustomerAccountNumbersByPrefix looks like this:

/// <summary>
/// Retrieves all AccountNumbers which match prefix (AutoComplete)
/// </summary>
/// <param name="prefix">Prefix to match</param>
public string[] GetCustomerAccountNumbersByPrefix(string prefix)
{
    var customers = from e in db.Customers
            where e.AccountNumber.StartsWith(prefix)
            orderby e.AccountNumber
            select e;

    int count = customers.Count();
    string[] result = customers.Take(21).Select(x => x.AccountNumber + " – " 
                                         + x.NameAlias).ToArray<string>();

    if (result.Count() > 20)
    {
       result[20] = ">> antal: " + count.ToString();
    }

    return result;
}


If the ResultSet contains more than 20 rows I want to provide information about this to the user, displaying the total number of rows, which is why I do a Count() before Take()

I use .Select() to transform the properties AccountNumber and NameAlias into a single string, which is then returned as a string[] using ToArray(), in the process I limit the number of rows to a maximum of 21 using Take()

And all there’s left to do is to optionally change the 21th entry to show the total number of rows information. As the string[] is fixed it’s much easier simply to take out an extra row - Take(21) – and use this last row to show the information.

Hope the above example is useful to others who might want to use the AutoCompleteExtender in a UserControls only to find out that the PageMethod actually DO have to reside in the .aspx.cs file.

LINQtoSQL – Grouping and selecting

I have a Order->OrderLine table setup where I need to check if any “duplicate” orders exists. In order to check this I need to be able to select select various columns *not* contained in the group (key) – playing around in LinqPad I quickly came up with this query:

from line in AxSaleLines
where line.QuantityRemaining > 0
group line by new {line.AxSale.AccountNumber, line.ItemNumber, 
ProdName = line.Product.Name, ProdFormat = line.Product.ProductFormat.Name}
into myGroup
where myGroup.Count() > 1
from s in AxSaleLines
where s.ItemNumber == myGroup.Key.ItemNumber && 
      s.AxSale.AccountNumber == myGroup.Key.AccountNumber
select new { s.AxSale.SalesId, s.AxSale.AccountNumber, 
             CustomerName = s.AxSale.Customer.Name, s.ItemNumber, 
             ProductName = s.Product.Name, s.Product.ProductFormatName, 
             s.Quantity, s.AxSale.SalesOrigin}
 


When bound to a grid, I can then show this to the user:

groupedgrid

Register Custom Controls In Web.config

If you’re using a lot of UserControls or a control package, you’re probably finding yourself adding tons of <%@ Register… %> tags to your .aspx pages.

Phil Haack has an excellent post about how to avoid all this mess and put your assembly references into the Web.Config file. Go check it out!

Slide Screen for Android

slidescreenui If you’re an Android fan (as I am) and thinks the stock homescreen is really not that great/usable, you should take a look at SlideScreen

SlideScreen is a homescreen replacement, which totally changes how you use your phone.

The cool thing about SlideScreen is that it integrates information from various sources—Google Reader, Twitter, Facebook, email, text messages, calendar appointments and stocks—right onto the homescreen

If you’re a proud Android user, be sure to check it out!

ASPX Edit Helper Add-In For Visual Studio

Jacob mentioned a nice little free Add-In for VS2005/2008 yesterday while we we’re chatting on Messenger :-)

Short description: “The ASPX Edit Helper is an add-in for Visual Studio 2005 for those who like to type ASPX markup themselves instead of using the visual designer"

Usage:

Type <asp:Label and press Enter. The add-in will replace it with:

<asp:Label runat=”server” id=”asp_Label2943″ />

The add-in will also highlight the ID attribute value so you can immediately type a new ID for the control.

Very handy, thanks to Jacob for mentioning this – free download at: http://www.ardentdev.com/aspxedithelper

ASP.NET MVC Best Practices

Kazi Manzur Rashid has put together a blog post (in 2 parts) about ASP.NET MVC Best Practices which is worth reading if you’re working with ASP.NET MVC.

ASP.NET MVC Best Practices (Part 1)
ASP.NET MVC Best Practices (Part 2)

ASP.NET MVC and ASCX

Jeffrey Palermo has an inspiring article about templating and (why not) to use .ascx aka UserControls in ASP.NET MVC – I’ll definitely be using this approach:

http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/