Wednesday, April 7, 2010

Best Practices for Speeding Up Your Web Site

1. Minimize HTTP based Requests

2. HTTP Compression

3. Correct Formatted Images at the Right Place

4. Compress CSS, JavaScript and Images

5. CSS at Top and Javascript at bottom

6. Content Delivery Network: (CDN)

7. Ajax vs. Callback

8. Reduce Cookie size

9. Use Cache appropriately


10. Upload compiled code rather than source code

A good article by Adnan Aman Please check below

Tuesday, April 6, 2010

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

Please refer the following solution:

Note: If still it doesn't work You have to Impersonate a specific user in code.
This solved one of my problem where I was creating a Excel on runtime and saving it to server.

The problem is that by default Microsoft Excel as a COM object can only activated by the following accounts:

  • Administrator
  • System
  • Interactive
So you can impersonate section of code which needs the above privilege of a user and then you can undo the impersonate when you are done with it.

Here is the solution:

Problem Reading Excel Dates. Excel date gets converted to Double value in DotNet

Dates are stored in Excel as doubles, only displayed as dates per the formatting on the sheet. The Value2 property on the cells returns the stored long value. You need to convert this value to the date yourself. You can use the DateTime.FromOADate() method to do this conversion.

Example Code:


double dateTimeInExcelFormat = Convert.ToDouble(((Range)workSheet.Cells[5, 3]).Value2);
DateTime date = DateTime.FromOADate(dateTimeInExcelFormat);

How to kill Excel.exe process?

I was developing an application where I need to export data from an application page to a Excel sheet. I also required to import an existing Excel and reading the data int an object.
The things were going fine in order to import and export but there was a problem in terms of memory leak.
For all the operation you perform to open a Excel file or to write a Excel file a process Excel.exe is created and which does not get kill and it resides in the memory until you delete it manually.

I spent a lot of time just to find the solution. I tried many more solution but the one which worked for me was excellent and good enough. I thought lets share the solution here.

Here is the code:


Class Sample{

Hashtable myHashtable;

Private void ReadExcel()
{
CheckExcelProcesses(); // call this to find out all the processes instances running currently

//Open a Excel a file
//Do all operation needed

KillExcel();
}

private void CheckExcelProcesses()
{
Process[] AllProcesses = Process.GetProcessesByName("excel");
myHashtable = new Hashtable();
int iCount = 0;
foreach ( Process ExcelProcess in AllProcesses) {
myHashtable.Add(ExcelProcess.Id, iCount);
iCount = iCount + 1;
}

private void KillExcel()
{
Process[] AllProcesses = Process.GetProcessesByName("excel");
// check to kill the right process
foreach ( Process ExcelProcess in AllProcesses) {
if (myHashtable.ContainsKey(ExcelProcess.Id) == false)
ExcelProcess.Kill();
}
AllProcesses = null;
}


}

Calling ASP.Net server side events using javascript

Executing Client Side Script at Server Side

Guys, some time we need to call some javascript function from code behind. It is very easy to do with the following one line of code.
It can alert a message you required. For example a validation message.

1. Showing an alert message from server side
ScriptManager.RegisterStartupScript(this, typeof(Page), "test","alert('" + message + "');",true);


2. Opening a .aspx page in new window from server side using javascript.
Example code:




string jScript = "window.open('../Report/" + reportFileName + "','_blank','height=655,width=810,top=150,left=150,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,copyhistory=false')";

ScriptManager.RegisterStartupScript(this, typeof(Page), "Report",
jScript, true);

To generate fetch xml in CRM 4.0

To generate fetch xml using advance view. The following javascript code needs to be placed in Advance Find View.

javascript:void( new function(){ prompt("Fetch
Parameters:",getFetchParams());function getFetchParams(){ return
"FetchXml:\n" + advFind.FetchXml + "\n\n" + "LayoutXml:\n" +
advFind.LayoutXml + "\n\n" + "EntityName:\n" + advFind.EntityName +
"\n\n" + "DefaultAdvancedFindViewId:\n" +
advFind.DefaultAdvancedFindViewId } } )

How to convert FileStream to MemoryStearm?

We do require sometime to convert a FileStream to MemoryStream. This is basically needed you need to download an existing file from the file system.
Steps:

//Open the File into file stream
FileStream fileStream = new FileStream(Server.MapPath(fileName), FileMode.Open, FileAccess.Read, FileShare.Read);

//Create and populate a memorystream with the contents of the filestream object

private MemoryStream StreamToMemory(FileStream fileStream)
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);


memoryStream.Flush();
fileStream.Close();
memoryStream.Close();
return memoryStream;


}
// Download the file at client end

byte[] byteArray = mstream.ToArray();
mstream.Flush();


mstream.Close();
Response.Clear();


Response.BinaryWrite(byteArray);
Response.AddHeader("Content-Disposition", "attachment; filename=QatargasTimesheet.xls");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";

justtwoshare

http://justtwoshare.blogspot.com is only for developers who works with Microsoft Technologies like .NET, SQL Server, Office, Dynamics CRM and sharepoint.

I would be sharing the problems which I been through so it makes your life easier.
You would definitely like something which makes you feel good and comfortable in programming.