Friday, February 19, 2010

Some C# Coding Standards Should follow

Coding standards


  • Ø                          Naming Convention
1.       Use Pascal casing for Class name, Methods, Properties etc
                  Public void DisplayName()
                   {
                    }

2.       Use Camel casing for variables
string customerName
Int customerId

3.       Don’t use Hungerian notation
 string m_sName;
Int nCount;


  • Ø                      Always use meaningful names for variables, properties, Methods etc.
Like:- string customerName
Avoid :- string cust_Name


  • Ø                   Do not use underscore ( _ ) for local variables

  •                          Prefix Boolean variables, properties and methods with “is” or similar prefixes.

Private bool isFinished;


  • Ø          Namespace names should follow the standard pattern

...


  • Ø                   File name should match with class name.

For example, for the class HelloWorld, the file name should be helloworld.cs (or, helloworld.vb)


  • Ø               Always use Pascal casing in filename.


  • Ø             Don’t use variables like i,j in loop. Always use meaningful names

Wrong approach:
For(int i=0; i<10; i++)
{
}

Right approach:
For(int count=0; count<10; count++)
{
}

  • Ø                 Always put Comments for classes, functions, properties etc.

///Summary
/// description of class,methods
///created by : user
///created on : date
///last modified by : user
///last created on : date



Wednesday, February 17, 2010

Creating Excel Workbook using C#.net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Exl = Microsoft.Office.Interop.Excel;

public class createExcel
{

//Excel Objects
        Exl.Application excel;
        Exl.Workbook workBook;
        Exl.Workbooks workBooks;
        Exl.Sheets sheets;
        Exl._Worksheet workSheet;
        Exl.Range range;
        Exl.Range rangeCells;



public createExcel()
{
InitializeExcelObjects();
}

public void InitializeExcelObjects()
        {
            excel = new Exl.Application();
            workBooks = excel.Workbooks;
            workBook = excel.Workbooks.Add(Type.Missing);

            //sheets = excel.Sheets;
            //wSheet = (Exl._Worksheet)(sheets[1]);
        }


private int ExcelTemplate(string fileName, DataTable dtData)
        {
            int nmReturn=0, nmSheetIndex=1, nmColumnCount=1;
            StringBuilder strBuild = new StringBuilder();
            string sheetName = string.Empty;
            try
            {
        
                sheetName = dtData.Rows[0][1].ToString();
                sheets = excel.Sheets;
                workSheet = ((Exl.Worksheet)sheets[nmSheetIndex]);
                workSheet.Name = sheetName;
                range = workSheet.Cells;
                rangeCells = workSheet.Cells;
                for (int rowCount = 0; rowCount < dtData.Rows.Count; rowCount++)
                {
                    if (!sheetName.Equals(dtData.Rows[rowCount][1].ToString()))
                    {
                        nmSheetIndex++;
                        sheetName = dtData.Rows[rowCount][1].ToString();
                        workSheet = ((Exl.Worksheet)sheets[nmSheetIndex]);
                        workSheet.Name = sheetName;
                        range = workSheet.Cells;
                        rangeCells = workSheet.Cells;
                        nmColumnCount = 1;
                    }
                        //workSheet.Columns.ColumnWidth = 50;
                        rangeCells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
                        rangeCells[1, nmColumnCount] = dtData.Rows[rowCount][0].ToString();
                        nmColumnCount++;
                                   }
                string filePath = Application.StartupPath.ToString() + fileName;
                workBook.SaveAs(filePath, Exl.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Exl.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                excel.Workbooks.Close();
                nmReturn++;
            }
            catch (Exception ex) { nmReturn = 0; }
            return nmReturn;
        }
}

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।