Saturday, November 28, 2009

Partial Methods

Partial classes was a great feature added in .NET framework 2.0.
Mainly,this feature allows us to add a custom code, when working with auto-generated code after adding a new form, DataSet, web-service etc..

Microsoft has extended this feature and introduced partial methods in C# 3.0.
With partial method we can define a method signature in one part of a partial class and its implementation in another part of the same type.
It enables class creators to provide method hooks that developers may decide to implement,very similar to events provider-subscriber relationship.


public partial class Department
//partial method signature
partial void OnCreated();

public Department()

//could be placed in separate file.
public partial class Department
partial void OnCreated()

Partial method should comply with following rules:

1. Signatures in both parts of the partial type must match.

2. The method must return void.

3. No access modifiers or attributes are allowed. Partial methods are implicitly private.

You can find additional info about partial methods here:

Thursday, November 26, 2009

Handling Errors in SQL Server Transactions

I've decided to write this post, because of misunderstanding I had about default error handling while writting T-SQL statements enclosed within single transaction.

Generally, we put our T SQL statements in a transaction, in order to perform all tasks as an atomic unit of work.

For instance:


When we look at this block, we expect that if some statement raises a run-time error, the entire block is aborted, but it's not what actually happens.

By default, SQL Server aborts the erroneous statement only, and normally completes the rest of the statements.

I'm pretty sure, that it was not our intention :)

We have several solutions to deal with this situation:

1. Use try-catch block introduced in SQL Server 2005





2. Check @@ERROR variable after each statement and commit only when the value of @@ERROR is zero.

3. Turn on XACT_ABORT option - actually the most convenient way to achieve the desired behaviour:


This option simply tells to SQL Server to terminate and roll back the current transaction when a run-time error occurs.

You can find the detailed description of "SET XACT_ABORT" command here.

That's it...


Monday, November 23, 2009

Handling Data Concurrency Using ADO.NET

I've encountered this article during studying of concurrency mechanism implemented in SQL Server 2005.

I found it nice, because it discusses various concurrency aspects while using ADO.NET disconnected model.



Wednesday, November 4, 2009

Cross-Tab in Crystal reports


Cross-tabs are special objects, designed in a spreadsheet style format (such as excel), you can place in your Crystal Reports. The cross tab object refers to rows and columns on the grid as groups of data in order to generate a summary data. This provides the user with an advanced analyzing data tool supported by an easy to read and use report format.

Before starting with the explanation, here are some general facts:

- You cannot create a cross-tab without a summarized field.

- All columns in a cross-tab must be the same width.

- You can pivot cross-tabs (swap the position of the rows and columns) .

- Cross-tabs doesn’t support RTL.

To illustrate the need of such an object, consider the following example:
A marketing analyst wants to generate a report that will show how many products (in units) were sold in each US state and what was the total cost.

The simple (but not easy) way to generate such a report is to generate a report that is initially grouped by US state and within grouped by product type as appears in Table1.

It should be noted that this kind of report may cause a difficulty to the analyst in comparing between some totals. For instance, comparing between the total income from selling Mountain bikes and the total income from selling Kids bikes.

A much simple view of the totals could be achieved by filling a grid using cross tab object as appears in Table2.

It should be noticed that the display in Table2 is much easier to read and use and in fact, the comparison between the total income from selling Mountain bikes and Kids bikes is immediate (9850$ Vs. 15360$).

Cross-Tab object wizard

The wizard can be divided into two main sections- Data section and Design section. The data section is handled under the tab named "Cross-Tab" and the design section is handled in tabs named "Style" and "Customize Style".

The "Cross-Tab" tab is used to define the database fields or formulas that make up the rows and columns of the cross-tab. The "Style tab" lets you choose a predefined formatting style for the grid on the cross-tab. And, the "Customize Style" tab displays a large number of custom formatting options to precisely control the appearance of the cross-tab.

How do we choose the data to be displayed in the cross-tab?

The "Cross-Tab" tab contains several data cubes:

- The "Available Fields" cube displays a list of the available report fields (for display).

- The "Columns" cube displays the list of fields that should be presented as columns.

- The "Rows" cube displays the list of fields that should be presented as rows.

- The "Summarized Fields" cube displays the fields to be summarized in each cell.

The summarized fields in the previous example were "Total Income" and "Total sold units". It should be stated that a summarized field can also be placed in the row total or column total of the cross tab object.

The choice of the fields could be performed by "Drag & Drop" a field from one cube to another or by using arrows. The summarized type of each summarized field could be changed by clicking "Change Summary" button.

How the data source of the cross-tab report should be look like?

In order to display properly that data on the Cross tab report, each cell on the cross tab object should be represented in a different row. A General structure of each row on the data source should be looked like:

For example, we can consider the data source for the previous example:

This structure of the data source allows the cross tab object to build a dynamically rows and columns that are based only on the data source rows.
The cross tab object search for a set of distinct row-values and distinct column-values and based on those sets builds the relevant table in the report.

Example of a dynamically cross-tab report

An international shipping company delivers in each day commodity between countries. For control management purposes, the company wants to generate a monthly report that will display the number of deliveries that were taken in each month in each route. A route is a path from a specific country to a specific country.

1. The data source for this report is in the form:

2. The Cross-Tab object wizard:

- Rows cube: "Source Country".

- Columns cube: "Target Country".

- Summarized Fields cube: "Sum of Total Monthly Deliveries".

3. The Cross-Tab object design:


Cross-tab object is a neccessary tool that displays data in a spreadsheet format and allows us to generate a summary data for further analyzing.

I hope that this post will help you to get started with this powerful crystal report's feature.


Tuesday, November 3, 2009

Flags Attribute


Flags attribute is useful C# feature that allows treating of enumeration members as bit fields, therefore to combine multiple options within single enum variable by using bitwise OR operation.


We simply decorate our enum with Flags attribute and assign to each member number that follows base 2 sequence like 1, 2, 4, 8 and so on:

public enum MoneyPart

A = 1,
B = 2,
C = 4

Combine the appropriate options:

MoneyPart moneyPartVar = MoneyPart.A | MoneyPart.B;

Now we can add a method that receives MoneyPart variable and checks its value by using bitwise AND operation:

private decimal CalculatePolicyRedemption(MoneyPart moneyParts)
decimal total = 0M;
if ((moneyParts & MoneyPart.A) != 0)
total += 100;
if ((moneyParts & MoneyPart.B) != 0)
total += 200;
if ((moneyParts & MoneyPart.C) != 0)
total += 300;

return total;

Isn't it handy feature? :)