Tuesday, October 7, 2014

Accessing "enum" values in .NET

Accessing enum values are very interesting in this sense that if you directly call enum item then it will return the left part of equal (=) sign. If you convert the enum item into integer then you will get right part of equal (=) sign. Lets see an example.

public enum NeedRefreshDataSource
    {
        Yes = 1,
        No = 2
    }


Now,

if you write NeedRefreshDataSource.Yes then you will get - Yes which is NeedRefreshDataSource type value.

if you write NeedRefreshDataSource.yes.ToString() then you will get - "Yes" which is string type value.

if you write Convert.ToInt32(NeedRefreshDataSource.yes) then you will get - 1 which is integer type value.

if you write Convert.ToInt32(NeedRefreshDataSource.yes).ToString() then you will get - "1" which is string type value.




Happy Coding...


Monday, August 18, 2014

Export crystal report to Excel - some tips.

Developing a crystal report which will be exported into Excel is always tricky, time consuming and needs a lot of patience. There are a lot of blog posts, forum discussions, articles in the web about this matter. During last couple of days I was hanging with one of my report and at last I've figured out some tricks about exporting crystal report to excel. Here is some - 

1. Do not use your mouse to resize/align your header/detail fields. Always depend on Properties pane. make sure the height. width and left properties are always same of related header and detail fields.

2. Top property of header/detail fields should be always 0.

2. Do not use line object. It will create an extra excel row after export.

3. You can use (single) right and bottom border of detail field objects. and only right border of header objects. 

4. May be the report which will export to excel will not be presentable for clients. So create another presentable report for client which may have lines or brders or colors whatever you want.

5. After export into excel - may be your header or detail section will be created using more than one excel rows. This may not be a problem. But you can decrease the height of detail/header fields to accommodate in single excel row. 

6. Suppress (Drill Down) the report header, report footer, page header, page footer. Because they are value less in Excel.

7. In your data source do not keep null data in any field. At-least fill data with empty string/something not valuable like dash (-).

May be these tricks will help you. 



Happy coding!

(also published in codeproject.com)


Monday, August 4, 2014

Getting worksheet from workbook when there is no such worksheet exists.

During application-level excel add-in development sometimes we need to check whether the worksheet is null or not. Consider the following lines -

string myWorkSheetName = "a_worksheet_Name";
Excel.Workbook Wb= Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.Worksheet mySheet = Wb.Worksheets.get_Item(myWorkSheetName) as Excel.Worksheet;

or,

Excel.Worksheet mySheet = Wb.Worksheets[myWorkSheetName] as Excel.Worksheet;

The worksheets indexer ([]) 
or the get_Item method of Worksheets class does not return nullable object. In this case we can take advantage of LINQ -

 Excel.Worksheet mySheet = Wb.Worksheets.Cast().Where(w => w.Name == myWorkSheetName).FirstOrDefault();

If the worksheet does not found then mySheet will contain null.

Monday, June 2, 2014

MS Office Add-in development: Enable "Embed Interop Types".

Warning    1    A reference was created to embedded interop assembly 'c:\Program Files\Reference Assemblies\Microsoft\VSTO40\v4.0.Framework\Microsoft.Office.Tools.Common.dll' because of an indirect reference to that assembly created by assembly 'c:\Program Files\Reference Assemblies\Microsoft\VSTO40\v4.0.Framework\Microsoft.Office.Tools.Word.dll'. Consider changing the 'Embed Interop Types' property on either assembly.

Developers get this is common warning during developing add-in for Microsoft Office. Because the type equivalence feature is not enabled by default in a office project. To enable this feature go to properties of
-- Microsoft.Office.Tools.Common 
-- Microsoft.Office.Tools
-- Microsoft.Office.Tools.Word 
-- Microsoft.Office.Tools.Excel  
-- Microsoft.Office.Tools.Outlook - references and 
set "True" to "Embed Interop Types" property.

The type equivalence feature, embeds type information to the office solution. And enables the version independence at run time