Wednesday, 27 February 2013

Error log


using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Diagnostics;
using System.Text;


/// <summary>
/// Summary description for ErrorLog
/// </summary>
public class ErrorLog
{
   
    public ErrorLog()
    {
        //
        // TODO: Add constructor logic here
        //
    }
         public static bool WriteErrorLog(string strErrorFileName, string strErrorString)
    {
        /*---------------------------------------------------------------------------
        Function Name : WriteErrorLog
        Arguments  : strErrorFile, strError
        Return Type   : bool
        Description  : Write Error Log in Text File
        ---------------------------------------------------------------------------*/

        bool boolReturn = false;

        strErrorString = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + ": " + strErrorString.ToString();
        try
        {
            if (strErrorString.ToUpper().Substring(0, strErrorString.Length - 1) != "THREAD WAS BEING ABORTED")
                boolReturn = IsWriteLog(strErrorFileName, strErrorString);         //Write Error Log in Text File
        }
        catch (Exception)
        {

        }
        return boolReturn;
    }
         public static bool WriteErrorLog(string strErrorString)
         {
             /*---------------------------------------------------------------------------
             Function Name : WriteErrorLog
             Arguments  : strErrorFile, strError
             Return Type   : bool
             Description  : Write Error Log in Text File
             ---------------------------------------------------------------------------*/

             string strErrorFileName = @"~\ErrorLogs\" + (DateTime.Today.ToLongDateString());
             bool boolReturn = false;

             strErrorString = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + ": " + strErrorString.ToString();
             try
             {
                 if (strErrorString.ToUpper().Substring(0, strErrorString.Length - 1) != "THREAD WAS BEING ABORTED")
                     boolReturn = IsWriteLog(strErrorFileName, strErrorString);         //Write Error Log in Text File
             }
             catch (Exception)
             {

             }
             return boolReturn;
         }

    private static bool IsWriteLog(string strErrorFile, string strError)
    {
        /*---------------------------------------------------------------------------
        Function Name : IsWriteLog
        Arguments  : strErrorFile, strError
        Return Type   : bool
        Description  : Create Text file and write Errors.
        ---------------------------------------------------------------------------*/

        StreamWriter swErrorFile;
        FileInfo fiFileInfo;
        int intMaxLogSize;
        bool boolReturn;


        // Get maximum error log size from web.config, use default of 100K if not found.
        intMaxLogSize = 1000000;

        try
        {
            if (Directory.Exists(HttpContext.Current.Server.MapPath(@"~\ErrorLogs\")) == false)
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(@"~\ErrorLogs\"));
            }
            // Set reference to passed error file.
            fiFileInfo = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(strErrorFile + ".log"));

            if (fiFileInfo.Exists)
            {
                // If current error file larger than max size.
                if (fiFileInfo.Length > intMaxLogSize)
                {
                    // Copy current error log to new file appending '_old' to current name,
                    // overwrite file if already exist.
                    swErrorFile = fiFileInfo.CreateText();
                    fiFileInfo.CopyTo(swErrorFile + "_" + DateTime.Now.Date.ToShortDateString() + ".log", true);
                    // Open stream to overwrite current error log file.                      
                }
                else
                {
                    // Open stream to append to current error log file.
                    swErrorFile = fiFileInfo.AppendText();
                }
            }
            else
            {
                // Create and open stream to error log file.
                swErrorFile = fiFileInfo.AppendText();
            }

            //Write to error log and close stream
            swErrorFile.WriteLine(strError);
            swErrorFile.WriteLine();
            swErrorFile.Close();


            return boolReturn = true;
        }
        catch (Exception)
        {
            boolReturn = false;
        }
        return boolReturn;
    }



    #region Log Exception in Error Log
    public static void Log(Exception exception)
    {
        /*-----------------------------------------------------------------------------------------------
        Function Name   : Log
        Arguments    : exception
        Return Type     : void
        Description    : Logs the Exception in a log file which is generated in the name of the aspx page
        -----------------------------------------------------------------------------------------------*/
        string strFileName = DateTime.Today.ToLongDateString();
        string strErrorLogPath = (HttpContext.Current.Server.MapPath(@"~\ErrorLogs\") + (strFileName + ".log"));
        string strErrorSrcFileName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.CurrentExecutionFilePath);
        string strMethodName = new StackTrace().GetFrame(1).GetMethod().Name;
        string strUserName = HttpContext.Current.Session["LoginName"] == null ? " " : HttpContext.Current.Session["LoginName"] as string;
        string strErrorMessage;
        TextWriter twErrorFile;

        strErrorMessage = (DateTime.Now.ToString() + "\r\n"); ;
        strErrorMessage += "File Name     :" + strErrorSrcFileName + "\r\n";
        strErrorMessage += "Method Name   :" + strMethodName + "\r\n";
        strErrorMessage += "Error Text    :" + exception.Message + "\r\n";
        strErrorMessage += "Error Desc    :" + exception.StackTrace + "\r\n";
        strErrorMessage += "User Name     :" + strUserName + "\r\n";
        strErrorMessage += "-----------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n";
        twErrorFile = File.AppendText(strErrorLogPath);
        twErrorFile.WriteLine(strErrorMessage);
        twErrorFile.Close();
    }
    #endregion
}

Tuesday, 19 February 2013

andriod tutorial with eclipse

Installation andriod

Features of andriod




 Features of Andriod 4.0


Features of Andriod 4.2 and techniques to develop Software .
Platform Version
API Level
VERSION_CODE
Notes
9
8
7
6
5
4
3
2
Android 1.0
1






Executing Andriod App in Visual Studio 2010

Sunday, 17 February 2013

CSV File Upload

//Here txtCSVFilePath is the textbox

//Checking the wheather given path is correct or not
            if (txtCSVFilePath.TextLength == 0 || File.Exists(txtCSVFilePath.Text) == false)
            {
                MessageBox.Show("Please browse valid file");
                return;
            }
            FileInfo fiCSVFile = new FileInfo(txtCSVFilePath.Text);
         
//checking given file is csv file or not
            if (fiCSVFile.Extension.ToLower() != ".csv")
            {
                MessageBox.Show("Please browse valid CSV file");
                return;
            }
//DGAData is a class that contain some variables
            DGAData objDGAData;
            int intLine = 0;
            StringBuilder sbLineMessage = new StringBuilder();
            using (StreamReader sr = new StreamReader(fiCSVFile.FullName))
            {
//Checking given file consists records or not
                while (sr.Peek() > 0)
                {
                    intLine++;
                    objDGAData = new DGAData();
                    try
                    {
//Storing one line of csv file in strArrLine String Array
                        String[] strArrLine = sr.ReadLine().Split(',');
                        objDGAData.dateAdded = Convert.ToDateTime(strArrLine[0]);
                        objDGAData.H2 = int.Parse(strArrLine[1]);
                        objDGAData.CH4 = int.Parse(strArrLine[2]);
                        objDGAData.C2H2 = int.Parse(strArrLine[3]);
                        objDGAData.C2H4 = int.Parse(strArrLine[4]);
                        objDGAData.C2H6 = int.Parse(strArrLine[5]);
                        objDGAData.CO = int.Parse(strArrLine[6]);
                        objDGAData.CO2 = int.Parse(strArrLine[7]);
                        objDGAData.O2 = int.Parse(strArrLine[8]);
                        objDGAData.N2 = int.Parse(strArrLine[9]);
                        objDGAData.TDCG = objDGAData.H2 + objDGAData.CH4 + objDGAData.C2H6 + objDGAData.C2H4 + objDGAData.C2H2 + objDGAData.CO;
                        //objDGAData.TDCG = int.Parse(strArrLine[10]);
                        objDGAData.EquipID =Convert.ToInt32(cmbxCsvEquip.SelectedValue);          
}        
                       int id=DGADataServices.AddNew(objDGAData);