Wednesday, 20 November 2013

IIS Error- Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).


For this fallow the fallowing steps-

 Enable the ASP.NET authentication for default website (in IIS) then create Desktop folder at C:\Windows\System32\config\systemprofile\Desktop.

step1 :  Go to run type dcomcnfg 
Step2:   Click >Component services >Computes >My Computer>Dcom config> and select microsoft Excel Application> 
Step3:   Right Click on Microsoft Excel Application>Properties>Security Tab
Step4-Under-Launch and activation permission->Customise->Edit->Add--type-(IIS_IUSRS) 
-> ok -> Allow Permission-Local Lounch and Local Activation
Step5- Repeate Step4 for Under Access Permission

Reference--http://www.youtube.com/watch?v=ijfb3E_7t3o

IIS-Error Resolve--Microsoft Excel cannot open or save any more documents because there is not enough available memory or disk space. • To make more memory available, close workbooks or programs you no longer need. • To free disk space, delete files you no longer need from the disk you are saving to.


For this fallow the fallowing steps-
step1 :  Go to run type dcomcnfg 
Step2:   Click >Component services >Computes >My Computer>Dcom config> and select micro soft Excel Application> 
Step3:   Right Click on Microsoft Excel Application>Properties>Give Asp.net Permissions 
Step 4: Select Identity table >Select interactive user >select ok

Friday, 15 November 2013

Restrict Ajax Calender future and past date using javascript

<script type="text/ecmascript">

    function checkDates(sender, args) {
       // alert(sender._selectedDate + "    enter    " + new Date());
        if (sender._selectedDate > new Date()) {
            alert('Date must be less than Today');
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }
</script >

//HTML
 <asp:TextBox runat="server"  ID="dtForm"  Enabled="False" ></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="cal_Surveyor"
TargetControlID="dtForm" OnClientDateSelectionChanged="checkDates"  Enabled="True" Format="dd/MM/yyyy"></asp:CalendarExtender>

Wednesday, 30 October 2013

Print Data Table data into Excel in c#.net

NameSpace--

using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

Source Code--

 private void printExcelReport(DataTable dtFarmDetail)
        {
            //Declare Excel variable

            object misValue = System.Reflection.Missing.Value;
            Excel.Application xlApp = new Excel.Application();
            Excel.Worksheet xlWorkSheet = new Microsoft.Office.Interop.Excel.Worksheet();
            Excel.Workbook xlWorkBook;
            Excel.Range chartRange;

            //Declare value
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            int Rowcount = dtFarmDetail.Rows.Count;
            int Colcount = dtFarmDetail.Columns.Count;

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            //Write Header
            for (int p = 1; p <= dtFarmDetail.Columns.Count; p++)
            {
                xlWorkSheet.Cells[1, p] = dtFarmDetail.Columns[p - 1].ColumnName;
            }

            //Assing data in array
            object[,] arrFarm = new object[dtFarmDetail.Rows.Count, dtFarmDetail.Columns.Count];
            for (int i = 0; i < dtFarmDetail.Rows.Count; i++)
            {
                for (int j = 0; j < dtFarmDetail.Columns.Count; j++)
                {
                    string cellvalues = dtFarmDetail.Rows[i][j].ToString();
                    string tickcross = "";
                    if (j > 10)
                    {
                        if (cellvalues == "Yes")
                        {
                            tickcross = "ü";
                        }
                        if (cellvalues == "No" || cellvalues == "")
                        {
                            tickcross = "û";
                        }                      
                    }
                    else
                    { tickcross = cellvalues; }

                    arrFarm[i, j] = tickcross;
                }
            }

            //Write data in sheet
            if (dtFarmDetail.Rows.Count > 0)
            {
                xlWorkSheet.Range[xlWorkSheet.Cells[2,1], xlWorkSheet.Cells[dtFarmDetail.Rows.Count, dtFarmDetail.Columns.Count]] = arrFarm;
                dtFarmDetail.Rows.Clear();
            }

            //Color header
            chartRange = xlWorkSheet.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[1, Colcount]);
            chartRange.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[1, Colcount]).Font.Bold = true;
            chartRange.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[1, Colcount]).Font.Size = 12;
            chartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);
           
            //Set Alingment
            chartRange = xlWorkSheet.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[2 + Rowcount, Colcount]);
            chartRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;

            //Assign font and size
            chartRange = xlWorkSheet.get_Range((object)xlWorkSheet.Cells[1, 1], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]);
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).Font.Name = "Wingdings";
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).Font.Bold = true;
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).Font.Size = 12;
            chartRange.get_Range((object)xlWorkSheet.Cells[2, 12], (object)xlWorkSheet.Cells[11 + Rowcount, Colcount]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;

            //write sheet
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideHorizontal).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideVertical).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).Weight = Excel.XlBorderWeight.xlThin;
            chartRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).Weight = Excel.XlBorderWeight.xlThin;
            xlWorkSheet.Cells.EntireColumn.AutoFit();

            xlApp.Visible = true;
            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);
            xlWorkSheet = null;
            xlWorkBook = null;
            xlApp = null;
            System.GC.Collect();
       
        }

Saturday, 19 October 2013

Create Log file in asp.net


On Event--
 string thisDir = Server.MapPath("./ExLogFile");
            //String Format : DateTime,Action,Userid,UserRole,Status,Attempt, FileName,empid,empname, username, adminactivity
            string logMessage = DateTime.Now.ToUniversalTime() + "," + "Logout," + Session["UName"] + "," + Session["user_RoleName"] + "," + "logged out" + "," + "," + "," + "," + "," + "," + "," ;
            writeToLogFile(logMessage, thisDir);


Method-


 public static void writeToLogFile(string logMessage, string thisDir)
    {
        StreamWriter swLog=null;
        try
        {
            string strLogMessage = string.Empty;
            string strLogFile = "" + thisDir + "\\logFile.txt";
         
            //strLogMessage = string.Format("{0}: {1}", DateTime.Now, logMessage);


            strLogMessage = string.Format(logMessage);

            if (Directory.Exists(thisDir)) { }
            else { System.IO.Directory.CreateDirectory(thisDir); }

            if (!File.Exists(strLogFile))
            {
                swLog = new StreamWriter(strLogFile);
            }
            else
            {
                swLog = File.AppendText(strLogFile);
            }

            swLog.WriteLine(strLogMessage);
            swLog.WriteLine();
            swLog.Close();
            swLog.Dispose();
        }
        catch (Exception)
        {
            swLog.Dispose();
            throw;
        }
        finally { swLog.Dispose(); }

    }

Single User login at a time any where with cache in asp.net

On Login Page-
Globle Define--  public System.Data.DataTable dtSessionGlobal = new DataTable();

  if (Cache["UserSessions"] != null)
        {
            dtSessionGlobal = (DataTable)Cache["UserSessions"];
        }
        else
        {
            dtSessionGlobal.Columns.Add("UserName");
            dtSessionGlobal.Columns.Add("SessionID");
            Cache["UserSessions"] = dtSessionGlobal;
        }

        EventArgs ee = new EventArgs();
        if (Session["username"] == null)
        {        
            btnsessionout_Click(sender, ee);
            //Response.Redirect("~/SignIn Users", false);
        }


On Logout Button-

 protected void btnsessionout_Click(object sender, EventArgs e)
    {
        string UserName =Session["UName"]==null ? "" : Session["UName"].ToString();
        Application[UserName] = null;
        if (dtSessionGlobal.Rows.Count > 0)
        {

            System.Data.DataRow[] dr = dtSessionGlobal.Select(" UserName='" + Session["UName"] + "'");// AND SessionID='" + Session.SessionID + "'
            if (dr.Length > 0)
            {
                foreach (DataRow drRow in dr)
                {
                    dtSessionGlobal.Rows.Remove(drRow);
                }
            }
        }
     
        Cache["UserSessions"] = dtSessionGlobal;

    }




At All Pages-

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtSessionGlobal = new DataTable();
        if (Cache["UserSessions"] != null)
        {
            dtSessionGlobal = (DataTable)Cache["UserSessions"];
        }
     
        if (!IsPostBack)
        {
            if (dtSessionGlobal.Rows.Count > 0)
            {
                System.Data.DataRow[] dr = dtSessionGlobal.Select(" UserName='" + Session["UName"] + "' AND SessionID<>'" + Session.SessionID + "'");
                if (dr.Length > 0)
                {
                    Response.Redirect("~/SignIn Users");
                }
                else if (dtSessionGlobal.Select(" UserName='" + Session["UName"] + "'").Length == 0)
                {
                    Response.Redirect("~/SignIn Users");
                }
            }
            else
            {
                Session.Abandon();
                Response.Redirect("~/SignIn Users");
            }
}

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...