Saturday, May 18, 2013

Ways of object serialization using C#


       Here i discuss how to serialize object state to a file in different formats, showing you 5 different serialization formats plus how to read object from config file:
  • Binary
  • XML
  • JSON
  • Base64
  • DataContract
  • Object in config file
I took a simple 'Person' class to be used for serializations and 'ConfigPerson' class for object read from config file. I used 'ConfigPerson' class for config read, since 'Person' class cant be used for config specific implementation.

For serialization process i created a component called 'Serializer' with below classes. 
'ObjectSerialize' class contains the nested classes for each serialization format and access methods for these classes. Each nested class inherits from abstract base class 'BaseFileChecker' (for logic reuse: file create & open) and implements 'IFormat' interface (for methods decouple: serialize & deserialize).
Check each nested class for format specific code.



For Testing this component i created a console application.
Pressing 1 shows the below result which is showing 'ConfigPerson' object read from app.config file.
Pressing any other key, prompts for serialization formats (applied on 'Person' class) 

When completed all 5 formats, you will see below files in your folder (folder name hardcoded in console project, change it for your requirement).

 Share your thoughts, alternative solutions and other serialization formats in below comments.

References:
stackoverflow.com/questions/945495/how-can-i-add-a-type-constraint-to-include-anything-serializable-in-a-generic-me
http://www.codeproject.com/Articles/1789/Object-Serialization-using-C
http://stackoverflow.com/questions/980202/how-do-i-find-the-current-executable-filename
http://forums.asp.net/t/1079061.aspx/1

Download:
Download the project solution as Zip file.

Saturday, April 13, 2013

Drill through rdlc report with empty dataset

 Hi,
    Here i will explain how to create a simple rdlc drillthrough report with filtering option and dynamic datasource/dataset updated with custom object collection programmatically.

Master Report
Drillthrough Report
Below are the step by step explanation to accomplish:
  • Create a web project from VisualStudio.
  • Add a rdlc file and name it LocalReport.rdlc
  • Add a typed dataset (.xsd) and name it DataSet1.xsd
  • Add two tables manually (right-click select from designer) with schema as in below image.


  • Now open LocalReport.rdlc designer, Add a Table Control by dragging it from toolbox onto report body.
  • From Report Data pane, click New -> Dataset, from the wizard, set: Name: DataSet1    Data source: DataSet1    Available datasets: Emp      then click ok. (check image below)

  • Now Report Data pane shows DataSet1 with its fields. Drag these 3 fields onto report table one by one and set its (first-row) header with background color.
  • Add textbox control for Report Title with value - Emp Details and set its background, foreground colors from its properties.
  • Add a parameter: from Report Data pane, click New -> Parameter and name it - pDesignation

  • Select report table, right-click for Tablix properties, select Filters option and set its fields as per below image.

  • Add a class file to the project and name it Entities.cs
  • Add 2 classes - Emp, EmpDetails with properties similar to tables schema in DataSet1.xsd
  • Also add static class DataProvider with 2 methods which returns collection of above types.(check code in download files)

  • Add an aspx file to the project and name it LocalReport.aspx, add a dropdownlist for filtering and ReportViewer controls.
  • Assign the LocalReport.rdlc to ReportViewer from webpage in designer mode.
  • At code-behind, update the Datasource with custom Emp Collection from DataProvider and assign it to report.
  •  Update the dropdownlist with unique Emp Designations and handle change-event to update report.
Till now we have completed single master report with filtering option.
You can give it a Run to test filtering.

Adding Drillthrough feature:
  • For Drillthrough we need another report, add a report to the project and name it SubReport.rdlc
  • Now open SubReport.rdlc designer, Add a Table control by dragging it from toolbox onto report body.
  • From Report Data pane, click New -> Dataset, from the wizard, set Name: DataSet1    Data source: DataSet1    Available datasets: EmpDetails           then click ok.
  • Now Report Data pane shows DataSet1 with its fields.
  • Drag 3 fields except EmpID onto report table one by one and set its (first-row) header with background color.
  • Add textbox control for Report Title with value - Emp Details (sub Report) and set its background, foreground colors from its properties.
  • Add a parameter: from Report Data pane, click New -> Parameter and name it - pEmpID
  • Select report table, right-click for Tablix properties, select Filters option and set its fields: Expression: [EmpID]      Operator: =     Value: [@pEmpID]
  • Now update the master report LocalReport.rdlc to support drillthrough
  • Open LocalReport.rdlc designer, right-click EmpID cell (not header), select Textbox properties -> Action and set fields as per below image.
  • Style the EmpID to blue color and decorate it with underline. 
  • Open LocalReport.aspx (in source mode) and add this attribute to the ReportViewer: ondrillthrough="ReportViewer1_Drillthrough" 
  • From its code-behind, write this code to handle drillthrough link.

Thats all, Now Run the application.

Note: Drillthrough reports are not linked reports.
Download: The Zip file and check the code.