Thursday, December 22, 2011

No Features Selected for Upgrade

Applies to: SQL Server 2005 Express Edition, SQL Server 2008 R2 Express Edition, Windows 64-bit.

Problem Description
I have tried many times to upgrade the current SQL Server 2005 Express Edition instance installed in my computer to SQL Server 2008 R2 Express Edition, but the upgrade process always ended with the same error.
"There are no features selected for upgrade".

Cause
I was using SQL Server 2008 R2 Express 64-bit (SQLEXPR_x64_ENU) setup program to upgrade a 32-bit instance of SQL Server 2005 Express Edition. Setup cannot upgrade from 32-bit platform to 64-bit platform: cross-platform upgrade is not supported.

Solution
I ran SQL Server 2008 R2 Express 32-bit (SQLEXPR_x86_ENU.exe) setup program and setup was able to detect the existing 32-bit instance of SQL Server 2005 Express and upgraded it successfully.

Submit this story to DotNetKicks

Wednesday, December 21, 2011

Using required field validator with dropdownlist

The requiredfieldvalidator can be used for a dropdown list, you just need to set InitialValue property of the RequiredFieldValidator with the InitialValue of the DropDownList to make it work

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>--Select--</asp:ListItem>
    <asp:ListItem>First Item</asp:ListItem>
    <asp:ListItem>Second Item</asp:ListItem>
    <asp:ListItem>Third Item</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
      ControlToValidate="DropDownList1"
      ErrorMessage="Value Required!"
      InitialValue="--Select--">
</asp:RequiredFieldValidator>


Submit this story to DotNetKicks

Preventing your site from being indexed, the right way

The robots exclusion protocol (REP), or robots.txt is a text file webmasters create to instruct robots (typically search engine robots) on how to crawl & index pages on their website.
Some people use robots.txt files to prevent sites from being indexed and thus showing up in the search engines.
But robots.txt doesn't actually do the latter, even though it does prevent your site from being indexed.
So, if you want to effectively hide pages from the search engines, and this might seem contradictory, you need them to index those pages. Why? Because when they index those pages, you can tell them not to List them. There's two ways of doing that:

Using meta tag on every page
By using robots meta tags, like this:
<meta name="robots" content="noindex,nofollow"/>

The issue with a tag like that is that you have to add it to each and every page. That's why the search engines came up with the X-Robots-Tag HTTP header.

Setting up for a whole site using IIS
If your site is running on IIS you can do by adding a custom HTTP Header
IIS 6: right-click on the "Web Sites" folder > Properties > HTTP Headers
IIS 7: on the server home screen, click on HTTP Request Headers, choose "add"
Name: X-Robots-Tag
Value: noindex, nofollow
And it'd have the effect that that entire site can be indexed, but will never be shown in the search results.
So, get rid of that robots.txt file with Disallow: / in it, and use the X-Robots-Tag instead!

Submit this story to DotNetKicks

Monday, December 19, 2011

Convert MySQL to MS Sql Server

Recently I needed to convert some MySQL tables to MS Sql Server.
It’s not too hard convert them by hand, but it is oh so tedious…
Gath Adams proved to have a quick and simple solution for this: cut the mysql table, paste into his blog and press convert.
http://gathadams.com/2008/02/07/convert-mysql-to-ms-sql-server/

Submit this story to DotNetKicks

Friday, December 16, 2011

Redirecting Assembly Versions

When you build a .NET Framework application against a specific version of a strong-named assembly, the application uses that version of the assembly at run time. However, sometimes you might want the application to run against a newer version of an assembly. An application configuration file, machine configuration file, or a publisher policy file can redirect one version of an assembly to another.
The simplest, is to modify the assembly bindings to redirect one version of an assembly to another.
In the example below the bindingRedirect allows requests for version 1.0.0.0 of this particular assembly to be serviced by version 2.0.0.0. So long as we’re sure the deserializing functionality is compatible, this solves the problem without having to change any code.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <dependentAssembly>
 <assemblyIdentity name="Configuration" publicKeyToken="........." culture=""/>
 <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
 <codeBase href="C:\bin\Configuration.dll" version="2.0.0.0" />
 </dependentAssembly>
</assemblyBinding>

Submit this story to DotNetKicks

Thursday, December 15, 2011

Shuffle in Linq (Part 1)

A simple implementation for shuffling a list in linq.
public static class ShuffleExtensions
{
    public static IEnumerable<tsource> 
           RandomShuffle<tsource>(this IEnumerable<tsource> source)
    {
        var rand = new Random();
        return source.Select(t => new { 
                Index = rand.Next(), 
                Value = t })
            .OrderBy(p => p.Index)
            .Select(p => p.Value);
    }
}

Submit this story to DotNetKicks

How to programmatically set DataPager PageSize

DataPager1.PageSize = xx

Where xx is the size (Integer), you could put it in the Page_Init or Page_Load event, before any DataBind.
If your DataPager is within the ListView you'd have to find it first, and you could do that using the FindControl method.
If the DataPager is outside the Listview obviously, you don't have to find it using the FindControl method.
Also, If you're not using a data source (like the SqlDataSource) you would also provide the .DataSource property before you execute .DataBind on your Listview.

Submit this story to DotNetKicks

jQuery UI Sortable Tutorial

Here's a simple tutorial showing how to use jQuery UI's sortable plugin to update a database table's sort order field on the fly using ajax.
Here's the HTML markup used to define the content that will be sortable (note you don't need to use a ul as most examples will show, you can use any container, and its children will be sortable in this case we used divs.):

<div id="fruit_sort">
 <div id="fruit_1">Apple</div>
 <div id="fruit_2">Orange</div>
 <div id="fruit_3">Pear</div>
</div>

In order to make the fruit_sort container sortable, we need to run $('#fruit_sort').sortable(); at a bare minimum. But how can we update our database every time the list is sorted? To do this we must bind to the sortable update event. Accordign to the docs this event is triggered when the user stopped sorting and the DOM position has changed.
So here's the JavaScript we are going to use to make our fruit_sort work:

<script language="javascript">
 $(document).ready(function(){
  $('#fruit_sort').sortable({
   update: function(event, ui) {
    var fruitOrder = $(this).sortable('toArray').toString();
    $.get('update-sort.cfm', {fruitOrder:fruitOrder});
   }
  });
 });
</script>

By calling $(this).sortable('toArray').toString() in our example update event handler we are given a string list of all the item id's, it might look likefruit_2,fruit_1,fruit_3. We can then send the new order to our server in an ajax request using $.get()
Finally to handle things on the server side (using AspNet in this demo) we might have some code that looks like this sort.ashx:

/// <summary>
/// Summary description for SortProducts
/// </summary>
public class SortProducts : IHttpHandler
{
    private readonly ProductRepository _productRepository = 
            new ProductRepository();
    public void ProcessRequest(HttpContext context)
    {
        var productIds = context.Request["fruitOrder"].Split
           (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        var products = productIds.Select
           (productId => _productRepository.GetById(int.Parse(productId)))
            .ToList();
        int sort = 0;
        foreach (var product in products)
        {
            product.SortOrder = sort++;
            _productRepository.UpdateOnCommit(product);
        }
        _productRepository.CommitChanges();
        
        context.Response.ContentType = "text/plain";
        context.Response.Write("Success");
    }
    
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Submit this story to DotNetKicks