Saturday, 17 October 2015

Quick Look: Deduplicating Data with CTEs and ROW_NUMBER()

I recently ran into a common issue while querying data for a UI component: dealing with potential duplicate records in the database. To solve it cleanly without creating physical temp tables, I used a Common Table Expression (CTE) combined with T-SQL's ROW_NUMBER() window function.

What is a CTE?

A CTE (Common Table Expression) is essentially a temporary, named result set defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement.

Think of it as an inline, readable view that exists only for the duration of the query. Unlike #temp tables or @table variables, CTEs require no explicit drop commands or storage overhead in tempdb.

How ROW_NUMBER() and PARTITION BY Help

The ROW_NUMBER() function assigns a sequential integer to each row within your result set. When paired with PARTITION BY, it allows you to reset the row counter for different logical groupings.

For example, instead of numbering all rows continuously from 1 to 10 across your entire dataset, PARTITION BY CategoryId can number rows 1–5 for Category A, and reset back to 1–5 for Category B.

Putting It Together: The Deduplication Pattern

Here is how you can use a CTE and ROW_NUMBER() to identify and eliminate duplicate rows in a single step:

WITH DuplicateCTE AS (
    SELECT 
        CustomerId,
        Email,
        CreatedDate,
        ROW_NUMBER() OVER (
            PARTITION BY Email 
            ORDER BY CreatedDate DESC
        ) AS RowNum
    FROM Customers
)
SELECT 
    CustomerId, 
    Email, 
    CreatedDate
FROM DuplicateCTE
WHERE RowNum = 1;


How This Query Works:

  1. PARTITION BY Email: Groups rows sharing the same email address into their own partition.

  2. ORDER BY CreatedDate DESC: Ranks the newest record first (RowNum = 1), while older duplicate entries get assigned 2, 3, etc.

  3. WHERE RowNum = 1: Filters out all subsequent duplicates, instantly returning only the single most recent record for each email address to your UI!

Thursday, 4 June 2015

A closer look -Enumerable.Range (System.Linq)

Enumerable.Range is a small static method in the  Enumerable class from System.Linq. 

The Enumerable.Range() method performs a very simple function, but it’s results can be used to drive much more complex LINQ expressions.  In it's simplest form it can be used to generate a sequence of consecutive integers. 

It accepts two int parameters, start and count, and constructs a sequence of count consecutive integers, starting at start:


  • Range(int start, int count)
  • Returns a sequence of int from start to (start + count – 1).


So, for example, the code snippet below will create an List<int> of the numbers from 1 to 10:
   1: var numbers = Enumerable.Range(1, 10).ToList();
   2: string nums= string.Join(",", numbers); 
  3: // result will be : "1,2,3,4,5,6,7,8,9,10"

Not just for numbers !

Or it can be used to  to generate a string containing the English alphabet
   1: IEnumerable<char> letters = Enumerable
   2:  .Range(0, 26)
   3:  .Select(x => (char)(x + 'a'));
   4: string alphabet = string.Join("", letters); 
   5: // result will be : "abcdefghijklmnopqrstuvwxyz"
Handy eh ?

Getting a bit more complicated now, if we wanted a list of the first 5 even numbers, we could start with the number of expected items and use the .Select() clause in conjunction with the mod operator to bring back only the even numbers :
   1: var evens = Enumerable.Range(0, 5).Select(n => n*2).ToList();
   2: string ans = string.Join(",", evens ); 
   3: // result will be : "0,2,4,6,8"
Alternatively , you can achieve the same result using the .Where() clause against the total results to filter it down:
   1: var another= Enumerable.Range(0, 10).Where(n => (n % 2) == 0).ToList();
   2: string ans = string.Join(",", another); 
   3: // result again will be : "0,2,4,6,8"
Or if you want to provide a min value, max value, and a number of steps, as demonstrated in this question on StackOverflow and answered by by John Skeet:

i.e. basically what was needed here was to return something like this :

0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000

Were the min was 0 , the max 1000 , and he wanted to get there in 11 steps.
   1: int numberOfSteps = 11, max = 1000, min = 0;
   2: var example = Enumerable.Range(0, numberOfSteps)
.Select(i => min + (max - min) * ((double)i / (numberOfSteps - 1)));
   3: string ans = string.Join(",", example); 
   4: // result will be : "0,100,200,300,400,500,600,700,800,900,1000"

Conclusion

The Enumerable.Range() method performs a very simple function, but it’s results can be used to drive much more complex LINQ expressions.

Friday, 29 May 2015

Displaying SSRS Report in MVC using MvcReportViewer -Part II

Embedding a Local Report


In my previous post we discussed how to install the MVCReportViewer package via Nuget into your application to avail of the helpers to embed SSRS reports into your MVC views.

In this article we will discuss using the MVCReportViewer  package and helpers to embed local reports.

Most often people want to embed SSRS reports from the server into thier views, but sometimes we just want to create a simple local report in Visual Studio and display that in our view. This is the scenario we will discuss below.

Creating reports in Visual Studio 2012/2013

Visual Studio 2012/2013 comes with reporting capibilites out of the box. Simply right click in your solution explorer in Visual Studio and Add Item/ Reporting and you can see you have the option to add a Report or use the Report Wizard.



















This provides you with good basic reports, it's like a pared down version of the tools and controls you get with real SSRS. If this suits your needs you need go no further. However if you need more capilibilites than whats on offer here you will need to install the SQL Server Data Tools – Business Intelligence tools  for Visual Studio 2012/2013.

SQL Server Data Tools – Business Intelligence for Visual Studio 

Currently the URL to download Microsoft Visual Studio Tools for Applications component for Visual Studio 2012 can be found here:

http://www.microsoft.com/en-ca/download/details.aspx?id=38807

Once you have install it, you will be able to create and work with SSRS reports within Visual Studio. Here are the basic steps for setting up a new SSRS report project:

Open up Visual Studio

  • Select File –> New –> Project
  • You will now see a new project template option called Business Intelligence
  • Expand this project template and select the Reporting Services option.
  • You can now either choose to use the setup wizard or just a plain project template.


Regardless of whether your requirements necessitate the use of the Business Intelligence tools or the standard out of the box reporting tools in Visual Studio, lets now disucss adding a basic report to your project so you can embed it in your MVC view.

DataSets

Before creating your report, you need to define your dataset. Lets create a Report folder in our project. It is a good idea to organise your folder structure within your application in a logical manner , so we will use this folder for all our Datasets and reports.

Using the standard out of the box reporting functionality in Visual Studio 2012/2013, right-click in your Report folder and Add New item.

Go to Data-> and select Dataset. Call it 

Displaying SSRS Report in MVC using MvcReportViewer -Part I

In this article we will discuss installing the MVCReportViewer package into your MVC application to get it ready to embed reports. 

In Part 2 , we will discusss embedding local reports into your MVC application and in Part 3 we will discuss the changes needed to embed reports from yourn SSRS server.

Embedding SSRS or indeed local RDLC reports into a  view in an ASP.NET MVC application is easy using the MVCReportViewer package , and the HTML Helpers it comes with.

The first step to get the MVCReportViewer package installed into your project , to do this you can use the Package manager console in Visual Studio , using the following command:

PM> Install-Package MvcReportViewer
This will add the relevant scripts, Web.Config app settings and report viewer appx to your MVC Application which are needed to embed your reports into your views.

After sucessfully installing the MVCReportViewer package you will notice some changes to your application. 

The following App settings for the report viewer have been added to your Web.Config:


<add key="MvcReportViewer.AspxViewer" value="/MvcReportViewer.aspx" />
<add key="MvcReportViewer.AspxViewerJavaScript" value="~/Scripts/MvcReportViewer.js" />
<add key="MvcReportViewer.ErrorPage" value="~/MvcReportViewerErrorPage.html" />
<add key="MvcReportViewer.ShowErrorPage" value="true" />

<add key="MvcReportViewer.ReportServerUrl" value="http://localhost/Reportserver_SQLEXPRESS" />
<add key="MvcReportViewer.Username" value="" />
<add key="MvcReportViewer.Password" value="" />
<add key="MvcReportViewer.EncryptParameters" value="false" />
<add key="MvcReportViewer.IsAzureSSRS" value="false" />

<add key="MvcReportViewer.LocalDataSourceProvider" value="MvcReportViewer.SessionLocalDataSourceProvider, MvcReportViewer" />


Key settings to note here , are the AspxViewer setting, which points to the aspx page which has also been installed by the NuGet package . It is this page that will be used to display our report in an iframe. 

The ReportServerUrl and LocalDataSourceProvider settings are important depending on wehther you want to embed a server report or a local one. I will use both later on when i give examples of both scenarios.

And that's it, now you are ready to embed a server or local report into your MVC views. See the following articles for examples of both.


Monday, 16 February 2015

Know It, Prove It

Anyone else doing the "Know It, Prove It" challenge on the Microsoft Virtual Academy site this February ? I'm half way through the Web Development challenge and really enjoying it.

Image result for know it prove it challenge

http://www.microsoftvirtualacademy.com/LearningPaths/Dashboard.aspx

Even if you're not taking part in the challenge, all the courses on the Virtual Academy are free and i highly recommend watching Learn-html5-with-javascript-css3-jumpstart-training with Michael Palermo and Jeremy Foster if you have a hour or two to spare. Its also recommended viewing for the Microsoft Exam 70-480 Programming in HTML5 with JavaScript and CSS3 if you're taking this Certification.

Thursday, 8 January 2015

Improving ASP.NET MVC Performance with Output Caching

You can dramatically improve the performance of your ASP.NET MVC application by taking advantage of the framework's built-in output caching mechanism. If you have a high-traffic page or an action that queries a large dataset from the database, output caching allows you to cache the HTML or JSON response directly in memory. By serving cached content for subsequent requests, you eliminate unnecessary database hits and drastically lower page load times.

In ASP.NET MVC 4 and 5, output caching can be implemented quickly using the standard [OutputCache] attribute without needing to build custom cache providers. Decorating your controller actions with this attribute gives you granular control over cache duration, location, and invalidation rules.

Default Caching Behavior in ASP.NET MVC

By default, GET actions in ASP.NET MVC return a Cache-Control header set to private. Consider a standard AJAX endpoint returning JSON:


public JsonResult Details(int id)
{
    // Fetch data for the specified ID
    var result = _repository.GetDetails(id);

    return Json(result, JsonRequestBehavior.AllowGet);
}

If you inspect the response headers using Fiddler or Browser Developer Tools, you will see output similar to this:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 4.0

Because Cache-Control is set to private, browsers (especially Internet Explorer) might cache these AJAX responses locally for the current user session. This can lead to stale data or unexpected application behavior when underlying data changes. Explicitly configuring output caching gives you predictability and control over client and server cache rules.

Enabling the [OutputCache] Attribute

To control caching explicitly, decorate your action method with the [OutputCache] attribute and specify a Duration (in seconds):


[OutputCache(Duration = 6)]


public ActionResult Search(string searchText)
{


return Json(SearchWebService.MyWebServiceInstance.AutocompleteSearchClient(searchText),JsonRequestBehavior.AllowGet);

}

Inspecting the HTTP response headers now reveals updated cache directives:

HTTP/1.1 200 OK
Cache-Control: public, no-transform, max-age=60
Expires: Mon, 29 Oct 2015 09:18:37 GMT
Last-Modified: Mon, 29 Oct 2015 09:17:37 GMT
Content-Type: application/json; charset=utf-8

Testing It in Visual Studio

  1. Remove the [OutputCache] attribute and place a breakpoint inside your action method.

  2. Launch your application and press F5 (Refresh) in your browser—the breakpoint hits on every request.

  3. Add [OutputCache(Duration = 60)] back to the action and refresh again.

  4. The breakpoint hits once for the initial request. Subsequent refreshes within the 60-second window serve cached content instantly without stepping into the controller action.

Handling Dynamic Content with VaryByParam

When an action returns different content based on query strings or route parameters, you must instruct the cache engine to store separate instances for each variation using VaryByParam.

  • VaryByParam = "none": Ignores all incoming parameters and serves the exact same cached payload to every user.

  • VaryByParam = "*": Creates a unique cache entry for every unique combination of query parameters.

  • Explicit Parameter List: Specify exact parameters separated by semicolons (e.g., VaryByParam = "accountId;page").

For example, when caching a paginated or sorted grid:


[OutputCache(Duration = 1, VaryByParam = "*")]
public ActionResult Index(int accountId, string page, string sort, string sortdir)
{

///code to get all accounts



return PartialView("_AccountsList", data);

}

Using VaryByParam = "*" guarantees that requests for ?accountId=1&page=2 and ?accountId=1&page=3 receive their corresponding cached views rather than returning the same cached data for both pages.

File upload in MVC3/4

Those new to MVC or coming from classic ASP.NET might be a little daunted when first tasked with uploading a file in MVC , as there's no concept of the fileupload control (or any server side control for that matter)?

Thanks to Darin Dimitrov for the great example here:

Your controller

public class HomeController : Controller
{
// This action renders the form
public ActionResult Index()
{
return View();
}
// This action handles the form POST and the upload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
}


The view


@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />

<input type="submit" value="OK" />
}