Sunday, December 11, 2011

Spring Bluff Railway Station


Spring Bluff is located in Queensland. It's about 130km from Brisbane and 14km from Toowoomba.

We went to Toowoomba carnival of flowers last September. We went to see Spring Bluff railway station on the way to Toowoomba.

The railway station is old and reminds me of railway stations in central hills in Sri Lanka. As we heard, Spring Bluff railway station is a heritage site and now mostly a visitor attraction. But they operate trains from Toowoomba railway station to Spring Buff railway station during the Toowoomba flower festival.

The railway station has a beautiful garden, picnic area and a cafe. If you are travelling through Toowoomba, this is a good place to visit and have a break.


Friday, September 23, 2011

Importing Excel with OleDbDataReader: handle cells with text length > 255

Most developers are familiar with importing excel sheets with OleDbDataReader. But not many know that Excel-OleDbDataReader cannot handle cells with text longer than 255 characters. This happens due to an intended behaviour in Excel.


When importing an excel sheet with the OleDBDataReader, it uses a registry setting called TypeGuessRows  to decide how many rows to inspect to guess the type of a column. Usually this setting is 8 rows. 


By changing that setting to zero forces Excel to check all the record to determine the column type:




The path to the registry key:







Remember:
1. You need to change the registry setting in correct Excel version ( e.g. 8.0, 12.0 etc).
2. If you are in a 64 bit machine, remember this Wow4232Node in the path.






Sunday, March 13, 2011

Repeatable Unit Tests with TransactionScope

In the beginning of most projects developers write unit tests. But when project grows it become difficult to manage the unit tests. Tough deadlines are the main culprit for non-maintained unit tests.

But poor test data management strategy is also a main culprit in non-maintainability of test cases. In a previous project I used TransactionScope avoid writing test data and causing un-manageable test cases

Strategy:
The simple theory is, if we rollback the transaction, test cases will not save any test data to the database. So what I have done is:
  1.  Used a single class to provide test data (yes I used a fixed set of data for testing, if someone needs to generalized this, he/she can use a separate test data in a .XML file or another storage)
  2. Wrote all database related functionality inside a TransactionScope and did a transaction rollback to avoid data save
Steps:
Used a single class to retrieve test data:
    public class TestDataProvider {
        public static Employee GetEmployee() {
            var employee = new Employee{
                Name = "Paul",
                DateOfBirth = new DateTime(1980, 5,5),
                JoinedDate = new DateTime(2005, 01, 01),
                Position = "Engineer"};
            return employee;
        }
    }   

Wrote all the database related functionality inside a transaction scope and did not commit the rollback:

    /// <summary>
    ///A test for AddEmployee
    ///</summary>
    [TestMethod()]
    public void AddEmployeeTest() {
        var employee = TestDataProvider.GetEmployee();
        using(var ts = new TransactionScope()) {
            int newEmployeeID = EmployeeLogic.AddEmployee(employee);
            Assert.AreNotEqual(0, newEmployeeID, "Employee Not Saved Properly");

            var saved = EmployeeLogic.FindByID(newEmployeeID);
            Assert.AreNotSame(saved.Name, employee.Name, "Different Name");
            Assert.AreNotSame(saved.DateOfBirth, employee.DateOfBirth, "Different DoB");

            //ts.Complete(); - do not complete the transaction
        }
    }

You should remember not to commit the transaction. Then the tests will run successfully without keeping any garbage data in database.

Note:
If you have identity data columns for your database tables, these test methods will increase the current identity value since it creates data rows in the database (data will be rollback, but not the identity value of the table). So you may need to execute a script to reset identity column value.

Sunday, February 20, 2011

Data Sorting with Dynamic/Conditional ORDER BY Statements


Almost in every web and windows application, users need to view data sort by various parameters. This is a very common scenario but there are many (yeah, too many) ways of solving this.

Some developers prefer to sort items within in the application code, some in database.  The correct decision depends on the nature of the problem.

I thought to demonstrate dynamic data sorting logic implemented in database side in this post. First of all, imagine we have to implement following business scenario:

Products are stored in AdventureWorks [Production].[Product] table. We need to sort products as below:

Strategy:
I am going to use dynamics ORDER BY functionality in SQL Server.

Steps:
Define a C# Enum to store the sorting options:

public enum SortBy {
    NameAtoZ        = 0,
    NameZtoA        = 1,
    PriceHighttoLow = 2,
    PriceLowtoHigh=3,
    SellStartDateHightoLow   = 4,
    SellStartDateLowtoHigh   = 5,
}

Build the drop down list to view sort options by assigning values of SortBy enum as values:

<asp:DropDownList ID="ddlProducts" runat="server">
   <asp:ListItem Text="Name (A-Z)" Value="0"></asp:ListItem>
   <asp:ListItem Text="Name (Z-A)" Value="1"></asp:ListItem>
   <asp:ListItem Text="Price (Low-High)" Value="2"></asp:ListItem>
   <asp:ListItem Text="Price (High-Low)" Value="3"></asp:ListItem>
   <asp:ListItem Text="Sell Start Date (High-Low)" Value="4"></asp:ListItem>
   <asp:ListItem Text="Sell Start Date (Low-High)" Value="5"></asp:ListItem>
</asp:DropDownList>

Pass the selected value of the drop down to the stored procedure. In the stored procedure, use dynamic ORDER BY clause to perform sorting:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE uspGetProductsSortBy
(
      @SortBy Int
)
AS
BEGIN
      
SELECT Name, ListPrice, SellStartDate
From Production.Product
ORDER BY         
   Case When @SortBy=0 Then Name END ASC,
   CASE When @SortBy=1 Then Name  END DESC,
   CASE When @SortBy=2 Then ListPrice END ASC,
   CASE When @SortBy=3 Then ListPrice END DESC,
   CASE When @SortBy=4 Then SellStartDate END ASC,
   CASE When @SortBy=5 Then SellStartDate END DESC   
   --Need to convert dates to string, otherwise result is not 100% correct
END
GO

References:

Maryborough: Our first stop in Australia


A scene from Ullula lagoon

Lenthall Dam Area


A close encounter with a whale
 
Maryborough Court House

Pub Fest 2010 - a group of participants
Fraser Coast Show 2010 
Fraser Coast Show 2010 - Wood chipping contest
Fraser Coast Show 2010 - Waiting for the rodeo

Maryborough City Hall


Maryborough is the city we are living currently. So I thought to write a blog post about the city and its surroundings.


Maryborough is in Queensland and it is located about 250km north of Brisbane, off the Bruce highway. It’s only 30km to Hervey Bay. Anyone visiting to Maryborough or Hervey Bay has several trasporation options:

  • By Car – Best option in this part of country.
  • TravelTrain – Maryborough has a railway station, anyone visiting Maryborough city or Hervey bay should get the coach connection from Maryborough station.
  • Coach/Bus - Tory’s tour coach – This runs twice a day from Hervey Bay to Brisbane and return. In my opinion this is the second best option to travel because it takes only 4 hours from Hervey Bay to Brisbane and 3.5 Hours from Maryborough to Brisbane.
  • 
  • Coach/Bus - Greyhound – They have many buses going through Maryborough both from Brisbane and to Brisbane. Greyhound’s travelling time is bit higher since it goes through all the cities on the route.
  • Plane – Hervey Bay has a airport which has connections to Brisbane, Sydney and Lady Elliot island.
  • Car Rentals/Taxi – After coming here, you can find many car rentals and taxis operating. So getting around is not difficult.
Maryborough is a self-styled heritage city in Queensland. According to the records, it started in 1847 and it had a port with ships coming from all over the world. Once it was suggested as the capital of the state (should be long ago).
 After all it’s busy days, now it is a clam and peaceful city with traces of history everywhere. Mary River is an important icon of the city with flowing through the city and making a scenic display. People here are very helpful and friendly, noticeably different than other places I have been.

The city has an information centre. Anyone visiting city should visit them, the staff is really friendly and helpful. They will give you a good insight to the city and surroundings, but I thought to list few things you can here:

  • See Maryborough city – see the old buildings, museums and the sleepy city. When you walk on the wharf street you might think you are walking in past.
  • Queens Park (at the edge of the river and next to the CBD) and Anzac Park/Ululah Lagoon – These make a relax feeling with it’s lush green. Also you can feed the ducks in Ullulah lagoon.
  • 
  • Thursday’s heritage markets – every Thursday Maryborough turns busy with it’s heritage market. It has a variety of stalls. Also the steam train and river cruise are operating on Thursdays.
  • Lake Lenthall– a popular recreational park just 20km north of Maryborough. It is in the Wongi forest and it is popular for fishing and camping.
  • Teddignton weir – This is a good picnic area next to the weir. I have seen people fishing on the dam, but the sign says not to. So, I am not sure about fishing in here. But it is a good picnic area with a small swimming pool for children and a patch of rainforest type trees.

And few more things you can do around here:
  • Fraser Island – most popular tourist destination in the region. We are yet to go there, but this is in the top of priority list.
  • Whale watching in Hervey Bay - Hervery Bay is a very good location to watch whales. We went on 'Spirit of Hervey Bay' ship and it was amazing to whales playing and swimming around.
  • Lady Elliot Island – Tourists can get flights from Hervey Bay airport to Lady Ellito Island.
  • Childers – Just 50 km north of Maryborough. There are few wineries near the city. Also Childers is popular for it’s multicultural festival held on last week of July each year.
  • Rainbow beach – it’s about 70km to Rainbow beach from Maryborough. Rainborw
Important events in Maryborough:

  • Sunday in the Park – on last Sunday of each month, Queens park turns in to a busy place with brass bands, stream trains (both mini train and historical stream engine) and lot of crowd. River cruise is also operates on Sundays.
  • Pub Fest – thousands of participants crawl from one pub to another to complete all the pubs in the pub trail. All of them wear decorated costumes and with music around, it’s a party day for Maryborough
  • Mary Popins Festival – Maryborough is the birth place of P.L.Travers, author of Mary Poppins children’s books. So, city has a one day celebration with various type of events which will take you to the time of Mary Popins.
  • Fraser Coast Show - this is an annual event with lot of activities, events, races and rodeos (last time they had different types of rodeos).
  • There are many more events in the regions. You can find everything in http://www.frasercoastevents.com.au/
There are many more places and things to do in Maryborough and Hervery Bay area. It is very easy to find details from an information center. Also I tried to add many informative links as possible. Feel free to comment/email your ideas and corrections. All are wellcome.

Friday, January 28, 2011

Starting a technical blog

I wrote a non-tech blog for few years. I tried not to write technical stuff. All those years I was just looking to understand what I need to do with blogging.

Finally I thought it's time to move to technical blogging. Just thought to start with day to day technical fixes I use. With time and posts, I may find what I should write.

Feel free to read and comment. Good to know that you are reading it.

BTW, I am continuing my non-tech blog @ pushpaka.blogspot.com. Feel fee to read it and comment it also.