Archive

Archive for May, 2010

Hide export format in RDLC/SSRS Report Viewer control

May 10, 2010 8 comments

I recently needed to hide or disable PDF format in Export Formats dropdown list displayed in SSRS ReportViewer’s toolbar in Local Report mode.

Unfortunately, ReportViewer control has no method or property to manage specific format’s visibility and this is true also for the recently released Report Viewer 10.

We can use Report Viewer’s property ShowExportControls to hide the export formats dropdown list but not a specific format such as PDF.

In Local Report mode, there are 2 export formats (Excel and PDF) and more Rendering Extensions for Server Report mode are available.

We have the option to hide export formats control and implement our own list. this article might help on this.

Finally, I ended up with adding new extension method SetExportFormatVisibility to ReportViewer control which uses private Reflection to disable/enable export formats.

This method is inspired by Stephen Songer’s blog post Disable/Enable export format in SSRS and ASP.NET.

The code consists of two parts, one is ReportViewerExtensions class which has SetExportFormatVisibility extension method and the second is ReportViewerExportFormat Enum to specify available export formats.

public static class ReportViewerExtensions
 {
 public static void SetExportFormatVisibility(this ReportViewer viewer, ReportViewerExportFormat format, bool isVisible)
 {

 string formatName = format.ToString();

 const System.Reflection.BindingFlags Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance;
 System.Reflection.FieldInfo m_previewService = viewer.LocalReport.GetType().GetField("m_previewService", Flags);

 System.Reflection.MethodInfo ListRenderingExtensions = m_previewService.FieldType.GetMethod("ListRenderingExtensions", Flags);
 object previewServiceInstance = m_previewService.GetValue(viewer.LocalReport);

 IList extensions = (IList)ListRenderingExtensions.Invoke(previewServiceInstance, null);
 System.Reflection.PropertyInfo name = extensions[0].GetType().GetProperty("Name", Flags);

 //object extension = null;
 foreach (var ext in extensions)
 {

 if ((string.Compare(name.GetValue(ext, null).ToString(), formatName, true) == 0))
 {
 System.Reflection.FieldInfo m_isVisible = ext.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

 System.Reflection.FieldInfo m_isExposedExternally = ext.GetType().GetField("m_isExposedExternally", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
 m_isVisible.SetValue(ext, isVisible);
 m_isExposedExternally.SetValue(ext, isVisible);

 break;
 }

 }
 }

 }

 public enum ReportViewerExportFormat
 {
 Excel,
 PDF
 }

Simple to use..

 ReportViewer1.SetExportFormatVisibility(ReportViewerExportFormat.PDF, false); 

Shout it kick it on DotNetKicks.com Retweet

Categories: ASP.NET Tags: , , ,

Windows 7 – Sticky Notes

Is it the time to leave my pen, sticky clips and such physical and paper-based stuff when I am writing my daily notes?

sticky-notes-menuWindows 7 introduced new feature Sticky Notes which enables Windows users to write their notes on the top of Windows Desktop.

You can open Sticky Notes through Start Menu => Accessories as you can see in the figure at the left.

As the below figure shows, User has the ability to create multiple notes and colorize each one of them with a different color (arrow 3).

Also user has the option to re-size a Note’s area separately from the other Notes.

To add a new Note click on + sign (arrow 1) or just press Ctrl + N. Also you can delete the active Note by clicking on X sign (arrow 2) or pressing Ctrl + D.

windows-7-sticky-notes

Enjoy…

Retweet

Categories: Windows 7 Tags: ,

Entity Framework and LINQ to SQL Paging

May 3, 2010 1 comment

In some applications when we need to display large sets of data , Data Paging is our choice in order to improve the performance. For ASP.NET developers we can use Data Paging mechanism provided by the most of ASP.NET Data-bound Controls such as GridView.

The default behavior of ASP.NET Data Controls is to bring the complete data set and do the paging at the application server side. This will led to network overload and server memory consuming every time user requests new data page.

Paging at the database level is mostly the best way to optimize the performance. The idea behind database level paging is to build the appropriate SQL query which brings the needed data page rather than the complete data set.

Entity Framework and LINQ to SQL provide developers with the ability to extend their SQL generator functionality to build paging enabled queries through extending IQueryable<T> interface implementation.

Here is the IQueryable<T> function as described in MSDN:

The IQueryable<T> interface enables queries to be polymorphic. That is, because a query against an IQueryable data source is represented as an expression tree, it can be executed against different types of data sources.

In this article, I am going to create a new Extended Method to offer Paging functionality to Entity Framework and LINQ to SQL query providers.

After this is completed we should be able to use Page method as the below figure shows

Page Method Signature

public static IQueryable<T> Page<T, TResult> (this IQueryable<T> query,
                                              int pageNum, int pageSize,
                                              Expression<Func<T, TResult>> orderByProperty,
                                              bool isAscendingOrder,
                                              out int rowsCount
                                             )

Page method takes 2 generic types T and TResult. T is the type of the data in the data source to be paged and TResult is the return value of the method encapsulated by Func<T, TResult> Delegate.

query parameter is the Object Query where paging should be applied, pageNum parameter is the data page to retrieve, pageSize is the number of rows to be retrieved per data page, orderByProperty is the T type property to sorted by and it is defined as a Lambda Expression to avoid passing orderByProperty parameter as a string, isAscendingOrder identifies if the sorting is ASC or DESC and  rowsCount is the count of rows in the complete data set. this will help on creating UI Data Pager in the Presentation Layer

rowsCount parameter is defined as out which requires rowsCount to be assigned to a value before the Page method returns.

Page Method Implementation

First off, I am validating the parameters as the following:

pageSize should be greater than zero, otherwise an Argument Out Of Range Exception thrown or a default value provided. The default value might be the rowsCount or a data page size predefined. In this example I’ll use 20 as the default page size.

pageNum should be greater than zero, otherwise pageNum is defined as 1. In case of pageSize is greater than or equals to rowsCount, 1 will be used as the pageNum.

I will use the query providers Take, Skip and OrderBy methods to get the correct data page according to the current page number and page size.

Let’s show how Skip and Take methods differ when mapped to SQL code. I am using LINQPad to get the equivalent SQL code.

If we write the below code

 Customers.Take(5)

Equivalent SQL code looks like

 SELECT TOP (5) [t0].[ID], [t0].[Name]
FROM [Customers] AS [t0]

And for

 Customers.Skip(5)

The result is

-- Region Parameters
DECLARE @p0 Int SET @p0 = 5
-- EndRegion
SELECT [t1].[ID], [t1].[Name]
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ID], [t0].[Name]) AS [ROW_NUMBER], [t0].[ID], [t0].[Name]
    FROM [Customers] AS [t0]
    ) AS [t1]
WHERE [t1].[ROW_NUMBER] > @p0
ORDER BY [t1].[ROW_NUMBER]

Page Method Complete Code

public static IQueryable<T> Page<T, TResult>(this IQueryable<T> query,
                        int pageNum, int pageSize,
                        Expression<Func<T, TResult>> orderByProperty,
                        bool isAscendingOrder, out int rowsCount)
{
    if (pageSize <= 0) pageSize = 20;

    rowsCount = query.Count();

    if (rowsCount <= pageSize || pageNum <= 0) pageNum = 1;

    int excludedRows = (pageNum - 1) * pageSize;

    if (isAscendingOrder)
        query = query.OrderBy(orderByProperty);
    else
        query = query.OrderByDescending(orderByProperty);

    return query.Skip(excludedRows).Take(pageSize);
}

excludedRows is the set of rows should be skipped using Skip method and computed as pageSize multiplied by (pageNum – 1)

I’d like to know your suggestions…

Shout it kick it on DotNetKicks.com Retweet

Follow

Get every new post delivered to your Inbox.