Saturday, May 05, 2012

Automatically Sizing Excel Columns

Today while trying to do some formatting on Excel using .Net I came up with an error.

One of the things I tried is to make Excel columns automatically size according to the content having on them. As all of you might know we can get this done in Excel by simply double clicking on the column’s right margin. While doing this in code I got the following exception.

ExcelSheet.get_Range("A1", "E10")' threw an exception of type 'System.Runtime.InteropServices.COMException'
base {System.Runtime.InteropServices.ExternalException}: {"Exception from HRESULT: 0x800401A8"}

The code involved in generating this error is as below.

  1. (ExcelSheet.get_Range("A1", "E10")).EntireColumn.AutoFit();

 

Later I found the reason for this error. Error will occur when we use AutoFit () on empty cells. Because initially I did not have anything in my excel sheet I kept on getting this. So to overcome this error use the same code to auto fit the cell contents simply after the cells are populated with values.

If you cannot get AutoFit () to work the reason might be the same thing. make sure the cells you apply auto fit have some values on them.

The best thing is to use AutoFit () after all data are entered into Excel sheet.

Monday, March 05, 2012

Dynamically Creating Computed Columns in SQL

If you are into programming you should have definitely worked with calculated fields in SQL. But for others I will briefly explain what they are.

Calculated columns are columns which depend on other columns. It gets its value by calculating which can involve values of other columns. The calculation formula is the only thing stored in the column.

So each time the column is referenced the calculation is done. But if you use the keyword “persisted” while creating the column then the values will be kept in the table. Whenever a referenced value is updated the computed column is also automatically updated to highlight the change. Also by using persisted you can index a computed column.

You can create a table with persisted computed column as follows.

  1. CREATE TABLE Customer
  2. (
  3. CustomerId INT IDENTITY(1, 1) NOT NULL,
  4. CustomerFirstName NVARCHAR(100) NOT NULL,
  5. CustomerLastName NVARCHAR(100) NOT NULL,
  6. CustomerFullName AS CustomerFirstName + ' ' + CustomerLastName PERSISTED
  7. )

One thing to remember is that you cannot directly insert values to a computed column.

I think you got a basic idea of computed columns. Now I would like to show how to create a computed column dynamically. For example think that you need to add a computed column to a table using a stored procedure. It is not a big deal.

I need to insert a TotalOrder column to the table named FoodOrder.

  1. CREATE TABLE FoodOrder
  2. (
  3. OrderId INT IDENTITY(1, 1) NOT NULL,
  4. OrderDate SMALLDATETIME NULL,
  5. CustomerName NVARCHAR(100) NOT NULL,
  6. TotalStarter INT NULL,
  7. TotalMainCourse INT NULL,
  8. TotalSoftBevarage INT NULL,
  9. TotalLiquer INT NULL,
  10. TotalDessert INT NULL
  11. )

This can be done using the following query.

  1. DECLARE @sExecuteCommand VARCHAR(250), --Keepa the command to be executed.
  2. @sColumns VARCHAR(150) --Keeps the columns to be included in the formula.
  3.  
  4. SET @sColumns = 'TotalStarter+TotalMainCourse+TotalSoftBevarage+TotalLiquer+TotalDessert+'
  5. SET @sExecuteCommand = 'ALTER TABLE FoodOrder ADD TotalOrder AS ' + SUBSTRING(@sColumns, 1, LEN(@sColumns)-1) -- Creating the computed column.
  6. EXEC (@sExecuteCommand)

Note that a cursor or a loop can be easily used to populate the variable “sColumns” with the columns required for the formula.

Hope this helps.

Monday, January 30, 2012

UPDATE with JOIN

If you need to update data of a table (DestinationTable) with data of another table (SourceTable) there are 3 ways you can get this done in SQL.

Method 1

This is the most common and simple way with using a sub query.





  1. UPDATE DestinationTable SET DestinationColumn =
  2.     (SELECT ValueColumn FROM SourceTable WHERE DestinationTable.MappingColumn = SourceTable.MappingColumn)




 

Method 2

This users the most common FROM clause to join the two tables as shown below.





  1. UPDATE DestinationTable SET DestinationColumn = SourceTable.ValueColumn
  2. FROM SourceTable
  3. WHERE DestinationTable.MappingColumn = SourceTable.MappingColumn




 

Method 3

Last method mentioned below uses the join clause to join both the tables to make the update happen properly.





  1. UPDATE DestinationTable SET DestinationColumn = SourceTable.ValueColumn
  2. FROM DestinationTable
  3. INNER JOIN SourceTable ON DestinationTable.MappingColumn = SourceTable.MappingColumn




 

Hope these helps.

Friday, January 27, 2012

Naracoorte Caves – Naracoorte South Australia

If you are a cave lover, there are many places Australia has to offer. Out of them one of the best is Naracoorte caves in South Australia. Inside the caves you will be able to see lime stones which took about 200 million years to form.

Stalactites – Are the ones hanging from above. Because they form from water dripping from above they tend to get points on them.

 20120127_17075020120127_170817

Stalagmites – Are formed on floor and start growing up with time.clip_image00220120127_16453020120127_164536

Columns – Are formed by one stalactite and stalagmite connecting after so many years.

 20120127_16574520120127_16425520120127_165836

It is good idea to plan and visit the caves bit early since you need time to have a look into the caves. They normally close at 5PM and the last tour starts around 4PM.

There is a visitor information centre in the Wonambi Fossil Centre so you can find out the interesting places in Naracoorte Caves National Park. Wet cave is a free attraction. When you purchase a ticket for a tour to visit Alexandra cave you will get free admittance to the Fossil enter and the miniature man made zoo.

Pram accessibility is not there when visiting caves, also you need to take care of your young children because they might need lot of assistance from you when inside the caves. Also supervise your children frequently since some of the Stalactites and Stalagmites are fragile and if they break one then thy may be destroying millions of years of natures’ work.

Car parking is free and plenty of sitting areas are there. Toilet facilities are there also is a restaurant so you can buy food if you need.

Address : Naracoorte Caves National Park, PO Box 134, Naracoorte SA 5271.

Phone : (+61 8) 8762 2340

EMail : naracoortecaves@sa.gov.au

Web : http://www.environment.sa.gov.au/naracoorte/Home

Map


View Larger Map

Wednesday, January 04, 2012

Deep Creek Conservation Park, South Australia

If you would love to enjoy scenery and wild life, a place you should definitely visit is Deep Creek conservation park in South Australia. It is located at the southern tip of the Fleurieu Peninsula.

It is always better to have a 4WD or an AWD but with a 2WD also in good weather you will be able to do most of the tracks without any major problems.


Map



View Larger Map

Monday, December 05, 2011

Rocking Horse – Gumeracha South Australia

When you come to South Australia another good place you should visit is the Rocking Horse at Gumeracha. There you will find a huge wooden rocking horse to which you can climb after paying few dollars. You can also watch the birds kept there and do not forget the Toy Factory nearby to do some shopping which has a good collection of gifts.

If any one interested visiting below are the contact details. But do not try to rock the horse.

Address
The Toy Factory,
Adelaide-Mannum Road,
Gumeracha, SA 5233,
South Australia,
Australia.

Telephone
(08) 8389 1085

Map


View Larger Map

Developing iPhone and Android Applications using C# .Net

Being a .Net developer did you think how great if you could develop applications for most popular mobile device platforms like iPhone and Android with the .Net skills you already have. If you were thinking like me then the wait is over.

Now with Mono you can create cross platform applications using your .Net framework skills. Mono is a software platform using which you can develop applications which runs on iPhones, Android devices, iPads and iPod Touches.

As per the Mono site, “The Mono Project is an open development initiative sponsored by Novell to develop an open source, UNIX version of the Microsoft .NET development platform.” If you are wondering about the name Mono, it is 'monkey' in Spanish.

You can try Mono for free by downloading the trial by going to the trial link. The good thing is after installing Mono you can use the more familiar Visual Studio to do the development using the available Mono templates.