Thursday, 4 April 2013

Page Used to Upload CSV File Using XML


/* ==========================================================================================
* Module Name           : Reports
* Purpose               : Middle layer between LoadPay and LoadPayBL
* Program Version       : 2.0
* Author Name           : Venkat P
* Table Used            : Nil
* View Used             : Nil
* Stored Procedures Used :
* Function/Trigger Used :
* Start Date            : 28/03/2011
* End Date              : MM/dd/yyyy
* ===========================================================================================
* TrackID      Date     Done By             Description
* ============================================================================================
*
* ===========================================================================================*/
using System;
using System.IO;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Configuration;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;

   
/// <summary>
/// Summary description for LoadPayBL
/// </summary>
public class LoadPayBL : LoadPayPL
{
    HOVSDBManager.HOVSDBManager objDB = new HOVSDBManager.HOVSDBManager();
    //LoadPayPL objLoadPL = new LoadPayPL();
    string sPayErrorLog = string.Empty;

    public string UploadCsvBL()
    {
        string sUploadMessage = string.Empty;// "Uploaded Successfully!";
        //        if (!Directory.Exists(UplaodCsv)) Directory.CreateDirectory(UplaodCsv);
        if ((FileUploadControl.PostedFile != null) && (FileUploadControl.PostedFile.ContentLength > 0))
        {
            string sFilename = System.IO.Path.GetFileName(FileUploadControl.FileName);
            FileInfo FileCSVInform = null;
            int iSuccessRecordCount = 0;
            int iErrorCount = 0;
            try
            {
                FileCSVInform = new FileInfo(@UplaodCsv + @"\" + sFilename);
                FileUploadControl.PostedFile.SaveAs(UplaodCsv + @"\" + sFilename);
                sFilename = UplaodCsv + @"\" + sFilename;
                string GetSystemIP = GetIPAddress();
                sPayErrorLog = HttpContext.Current.Server.MapPath(".") + @"/"
                               + Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["PayDataErrorLogPath"]);

                WriteLog("-----------------------------------------------------------------------------------------------------");
                WriteLog("                        Pay Upload Log");
                WriteLog("-----------------------------------------------------------------------------------------------------");
                WriteLog("Input File Name :" + sFilename);

                DataSet dsResultXML = new DataSet();
                DataTable dtResult = new DataTable();
                using (System.IO.StreamReader srFileUpload = new System.IO.StreamReader(sFilename.Trim()))
                {
                    string line = null;
                    int i = 0;

                    line = srFileUpload.ReadLine();
                    while ((line = srFileUpload.ReadLine()) != null)
                    {
                        string[] data = line.Split(',');
                        if (data.Length > 0)
                        {
                            if (i == 0)
                            {
                                foreach (var item in data)
                                {
                                    dtResult.Columns.Add(new DataColumn());
                                }
                                i++;
                            }
                            DataRow row = dtResult.NewRow();
                            row.ItemArray = data;
                            dtResult.Rows.Add(row);

                        }
                    }
                    dsResultXML.Tables.Add(dtResult);
                    string Systemip = GetIPAddress();                  
                    string _resInsert = string.Empty;
                    string[] _Month = Month.Split(',');
                    string test = dsResultXML.GetXml().Replace("'", "");
                    objDB.CreateParameters(6);                  
                    objDB.AddParameters(0, "@Resxml",  dsResultXML.GetXml().Replace("'",""));
                    objDB.AddParameters(1, "@LocationID", Location.Equals("3") ? (object) Location : DBNull.Value);
                    objDB.AddParameters(2, "@User", Convert.ToString(System.Web.HttpContext.Current.Session["UserId"]));
                    objDB.AddParameters(3, "@CreatedIP", Systemip);
                    objDB.AddParameters(4, "@Month", _Month[0].ToString().Trim());
                    objDB.AddParameters(5, "@Year", Year);
                    dsResultXML = objDB.ExecuteDataSet(CommandType.StoredProcedure, "usp_SetPayRollData_XML");
                    if(dsResultXML.Tables.Count >0)
                    {
                        for (int getdstablescount = 0; getdstablescount <= dsResultXML.Tables.Count-1; getdstablescount++)
                        {
                            if (dsResultXML.Tables[getdstablescount] != null)
                                _resInsert = dsResultXML.Tables[getdstablescount].Rows[0][0].ToString();
                            if (_resInsert.Contains(","))
                            {
                                _resInsert = _resInsert.Remove(_resInsert.LastIndexOf(','));
                            }
                            else if (_resInsert.Contains("EmpID :"))
                            {
                                _resInsert = _resInsert.Replace("EmpID :", "");
                            }                          
                            WriteLog(_resInsert);
                        }
                       
                        /*if (dsResultXML.Tables[0] != null)
                            WriteLog(dsResultXML.Tables[0].Rows[0][0].ToString());
                        if (dsResultXML.Tables[1] != null)
                            _resInsert = dsResultXML.Tables[1].Rows[0][0].ToString();
                        if (_resInsert.Contains(","))
                        {
                            _resInsert = _resInsert.Remove(_resInsert.LastIndexOf(','));
                        }
                        else
                        {
                            _resInsert = _resInsert.Replace("EmpID :", "");
                        }
                        WriteLog(_resInsert);
                        if (dsResultXML.Tables[2] != null)
                            _resInsert = dsResultXML.Tables[2].Rows[0][0].ToString();
                        if (_resInsert.Contains(","))
                        {
                            _resInsert = _resInsert.Remove(_resInsert.LastIndexOf(','));
                        }
                        else
                        {
                            _resInsert = _resInsert.Replace("EmpID :", "");
                        }
                        WriteLog(_resInsert);
                        if (dsResultXML.Tables[3] != null)
                            WriteLog(dsResultXML.Tables[3].Rows[0][0].ToString());*/
                        WriteLog("-----------------------------------------------------------------------------------------------------");
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }

            catch (Exception exErrorLog)
            {
                iErrorCount++;
                WriteLog("Error Record No: " + (iSuccessRecordCount + iErrorCount) + ". Error:" + exErrorLog.Message);
                if (sUploadMessage.Equals(string.Empty)) sUploadMessage = "Uploaded Successfully!. Found wrong entries, view log file.";

            }
        }

        return "Uploaded Successfully, Please View Logfile";                  
    }  
    private void WriteLog(string sMsg)
    {
        StreamWriter objWriter = null;
        try
        {
            //string sFolder = Server.MapPath("ExcelLog");
            if (Directory.Exists(sPayErrorLog) == false)
            {
                Directory.CreateDirectory(sPayErrorLog);
            }

            objWriter = new StreamWriter(sPayErrorLog.Replace(@"/", @"\") + @"\PayErrorLog" + DateTime.Now.ToString("yyyyMM") + ".txt", true);
            objWriter.WriteLine(DateTime.Now.ToString() + ": " + sMsg);
        }
        catch { }
        finally
        {
            if (objWriter != null) objWriter.Close();
        }
    }

    public string GetIPAddress()
    {
        string sIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (String.IsNullOrEmpty(sIP))
        {
            return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        else
            return sIP;

    }
    public DataSet GetLoadPayDetails()
    {  
        objDB.CreateParameters(4);
        objDB.AddParameters(0, "@LOCATIONID", Location == "0" ? (Object)"" : Location);
        objDB.AddParameters(1, "@PAYYEAR", Year);
        objDB.AddParameters(2, "@PAYMONTH", Month);
        objDB.AddParameters(3, "@DomainID", DomainID != "" ? (Object)DomainID : "");
        return objDB.ExecuteDataSet (CommandType.StoredProcedure, "USP_GETPAYDATA");
    }

}

No comments:

Post a Comment