Wednesday, January 22, 2014

CODE SNIPPET: Exception Class in C++

Here is a code snippet on how to create an exception class in C++

#include <exception>

class E : public exception {
const char * msg;
E(){};
public:
E(const char * s) throw() : msg(s) {}
const char * what() const throw() { return msg; }

};

Thursday, July 4, 2013

Creating a typed dataset in ReportViewer

CODE SNIPPET

I found this post that shows how to create a typed dataset. Thought I would share it here:

DataTable dt = new DataTable();
DataColumn dc = dt.Columns.Add();
dc.ColumnName = "DataColumn1";
dc = dt.Columns.Add();

dc.ColumnName = "DataColumn2";
dt.Rows.Add(new object[] { "Frank", 32 });
this.reportViewer1.LocalReport.DataSources.Clear();

this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_DataTable1", dt));
this.reportViewer1.RefreshReport();


Here is the original post

Thursday, June 27, 2013

Multilevel subreports in SSRS Reporting Services

CODE SNIPPETS, TUTORIAL

Microsoft's SSRS Reporting services allows 20 levels of subreports. A Subreport can be helpful when data from different datasets need to be embedded within lists, tables or matrix. When using business objects from memory, tables and lists do not allow inputting fields from different datasets. For eg: if a List is linked to a class Product, and you would like to embed a table within this list from a different class, SSRS won't let you do it. This is where subreports can be useful.

For this tutorial, I will create a main report and 2 levels of subreports.

  1. Create the main report MainReport.rdlc.
  2. Create two additional reports SubReport1.rdlc and SubReport2.rdlc
  3. In MainReport.rdlc, drag and place a Subreport placeholder from the toolbox. Right click to view the properties and under "Use this report as a subreport" textbox, write SubReport1. Note that the extension ".rdlc" is not written in this textbox
  4. Repeat step 3 in SubReport1 to create a second-level subreport
  5. Now we need to bind datasources and provide data to the subreports.
  6. Open Form.cs. Write the following lines in the Form1_Load() method before the this.reportViewer1.RefreshReport();
               
          reportViewer1.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
7. Now create a handler for the above method:

private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e){

//this should match the DataSet name given in .rdlc filee.DataSources.Add(new ReportDataSource("Grade", (this.bindingSource6)));e.DataSources.Add(
new ReportDataSource("Measurement", (this.bindingSource5)));}

Make sure the dataset name("Grade") matches the name given in .rdlc file.

8. Go to Form's designer and click on smartag. Choose Report1.rdlc to make this the main report.

Wednesday, June 5, 2013

When to use Inheritance, Composition, Interface, Aggregation

Composition

To identify composition associations, look for classes that cannot exist without other classes. Look for "wholes" and "parts". Ask, "Is this class part of another class?". The key question to ask is - Is this class destroyed when another class is destroyed?

Aggregation

The key difference between an aggregation and a composition is, in an aggregation, the part is not destroyed when the whole is destroyed. The part may be independent of the whole but the whole requires the part. For eg: A car class would require an engine class, chassis class. To identify aggregation, ask - Is this class part of another class and is it independent of the other class?

Inheritance

Inheritance provides the mechanism for new classes to be formed from the attributes and behavior of exisitng classes. Inheritance is best suited for relatively shallow class hierarchies. Use Inheritnace when:
  • The inheritance hieracrchy represents an is-a relaltionship and not a has-a(composition/aggregation) relationship.
  • The same behavior is applied to different data typoes.
  • The class hierarchy is reasonably shallow, and unlikely to become much deeper over time.

Interface

Like an inherited classm an interface provides a common specification for behavior, but, uunlike an inherited class, an interface cannot be created. The behavior has been abstracted out my any particular class and specifically implemented by all classes that need it.  Use interface inheritance when:
  • Unrelated object types need to provide the same behavior
  • Multiple inheritance is needed.
  • Inheritance is prohibited. For example C# structures cannot inherit from classes, but can implement interfaces.

Thursday, May 30, 2013

Resources for programmers and coders

I found these resources helpful while learning how to code:

  1. When to use classes? This article explains concepts of class, when to create a class, inheritance, composition and aggregation. It provides a visual approach and goes step-by-step from easy to harder concepts.
  2. C# Cheat Sheet: I made a cheat sheet when I was learning C#. It helped me remember common syntax, some important concepts and contains code snippets for a memory refresh.
  3. Learn something new: This website has tons of resources ranging from concepts on algorithms to practicing languages such as C# .You are sure to find something new to learn or brush up your skills http://www.codeproject.com/script/Content/SiteMap.aspx
  4. http://www.experts-exchange.com/tutorials/ tons of free tutorials.

Wednesday, May 29, 2013

Using business objects in Report Viewer in Microsoft Visual Studio - (problems with data sources, data bindings)

I recently started using Visual Studio to create reports from business objects ( objects in C#). However, I must admit, it is a bumpy ride trying to figure out all the nicks and nacks of Reporting Software and the resources available are quite scattered and all over the place. I hope this post will help you to overcome the problems that I had during the development phase.

To create a report using ReportViewer in Visual Studio:

1. Create a new Windows Forms Application Project. This creates a Form.cs file and Form designer file.
2. Add a class which will hold all business objects. Go to Project->Add Item->C# class.
3. Write the code for returning objects in this class file.
4. Now Add a New Item and click on Dataset. If you don't find it, it is probably hidden in the "Data" section.
5. Here you would want to literally draw out your dataset. Use the toolbox on the left to "Add a DataTable". The name of the Datable could be something sensible such as "Products", "Persons" etc.
6. Add methods that return values in the columns of this datatable. Keyboard shortcut is Ctrl + L to add a new column.
7. Once you have finished adding columns, you can create another datatable or hit "Save".
8. Now "Add another Item" and add a "Report" CAUTION: DO NOT USE REPORT WIZARD. CLICK ON REPORT (NOT REPORT WIZARD). DO NOT CLICK REPORT WIZARD. Got it? Good! :)
9. Now you would need to access your Report Data. If you do not already see a Sidebar titled "Report Data", click on View-> Report Data

Search This Blog