Code Project

Link Unit

Friday, August 28, 2009

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

If you add a reference to Microsoft Excel and then try to use it within your ASP.NET application you may receive the following errors.

Server Error in '/excel' Application. 

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

We referred this link for the DCOMCNFG settings required on Windows 2003 server http://blog.crowe.co.nz/archive/2006/03/02/589.aspx   . Even after the settings the problem was  not resolved and following error started to appear

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

Make some changes also in ASP.Net Site’s web.config file.

Given below is the settings for web.config file.

<identity impersonate="true" userName="DomainName\administrator" password="password"/>

            <authentication mode="Windows">           </authentication>

 

Tuesday, August 25, 2009

Four Little Known, Helpful Methods, Properties, and Features for ASP.NET Developers

The .NET Framework is big. Really big. The System.Web assembly, which contains the guts of ASP.NET, is comprised of nearly 2,000 types, over 23,000 methods, and more than 12,500 properties. And that's not counting any of the functionality added to ASP.NET since version 2.0. ASP.NET AJAX, the ListView control, Dynamic Data, URL routing, and other features add hundreds of new types and thousands of new methods and properties.

Given the size and scope of the .NET Framework and ASP.NET even there are certain to be dark corners for even the most experienced developers. There are certain classes, methods, and properties in the .NET Framework that every ASP.NET developer is intimately familiar with: the Request.QueryString collection; the Session object; Pageobject properties like IsValid and IsPostBack. Yet even in these familiar classes there are very useful and very helpful properties, methods, and features that are less widely known. Heck, I've been building ASP.NET applications and writing about ASP.NET functionality and features full time since 2001, and once or twice a month I still stumble across an unknown feature or a helpful property or method buried in some dark corner of the framework.

This article lists four helpful methods, properties, and features in the .NET Framework that, in my experience, are not widely known to ASP.NET developers. Read here

Auto Refresh in Chrome

I work with all type of Browser including the new entrant Chrome. Other browsers have some plugin or addon which help to auto refresh the page. So I tried to figure out Does Chrome have an auto-reload option (per tab) ?? 
The answer was google search away.

Just create a bookmark with the following code as the URL:

javascript:
timeout=prompt("Set timeout [s]");
current=location.href;
if(timeout>0)
setTimeout('reload()',1000*timeout);
else
location.replace(current);
function reload(){
setTimeout('reload()',1000*timeout);
fr4me='';
with(document){write(fr4me);void(close())};
}


Click the bookmark with the tab you want to auto-reload active.
Set the time interval (in seconds) or set it to zero to cancel auto-reload.

Hope it Helps
Jatinder Singh

Sunday, July 05, 2009

System.IO.DirectoryNotFoundException: Could not find a part of the path

In one of our project ,I was using a file upload control within to upload a file to Web Server and then do some processing on it.

When the file was uploaded to the server following error occurred:

System.IO.DirectoryNotFoundException: Could not find a part of the path C:\TMP\A.csv

In code I'm using postedfile.filename to get the filename and path. But it returns local path. I think the problem is that the fileupload control gets the path from the local machine, "C:\TMP" or whatever which is a local drive and same path / file might not exist at server side.

string strCSVFile = fUCSVFile.PostedFile.FileName.ToString(); // This returns path of the posted file ; i.e client path

System.IO.StreamReader reader = new System.IO.StreamReader(strCSVFile);
reader.Peek();
// Add the values in the DataTable By choosing the values which are separated by Comma
while (reader.Peek() > 0)
{
string words = reader.ReadLine();
/* Processing */

}

On local machine the code was working fine because sever and client are on SAME system.Hence it worked.


After changing the code like below, it started working fine on remote server too.

strCSVFile =Server.MapPath("~") + "\\" + System.Guid.NewGuid().ToString().Replace("-", "");
fUCSVFile.PostedFile.SaveAs(strCSVFile); // File is stored on the Server.

System.IO.StreamReader reader = new System.IO.StreamReader(strCSVFile);
reader.Peek();
// Add the values in the DataTable By choosing the values which are separated by Comma
while (reader.Peek() > 0)
{
string words = reader.ReadLine();
/* Processing */

}


Conclusion:
One should not use "PostedFile.FileName" for accessing the file; though it could be used for getting the filename.The problem of using "PostedFile.FileName" would be difficult to trace if the localpath exists on the server also.

The file should be saved by using "PostedFile.SaveAs(....)" and then accessed.

Hope it Helps.

Monday, May 25, 2009

FileHelpers : Strong type your flat file

We were working on a project, where it required to read a pipe separated values from the file. This is how we were doing it earlier

 

try

{

          StreamReader sr = new StreamReader(lstrFilePath);

          while ((lstrReadLine = sr.ReadLine()) != null)

          {

                    string[] lstrsplit = lstrReadLine.Split(new Char[] { '|' });

                    string lstrCOMP_NAME = lstrsplit[1];

                    string lstrFULL_NAME = lstrsplit[2];

                    string lstrFATHER_NAME = lstrsplit[3];

                    string lstrDESIGNATION = lstrsplit[4];

 

                    rowInsert["COMP_NAME"] = lstrCOMP_NAME;

                    rowInsert["FULL_NAME"] = lstrFULL_NAME;

                    rowInsert["FATHER_NAME"] = lstrFATHER_NAME;

                    rowInsert["DESIGNATION"] = lstrDESIGNATION;

 

                    dtANX.Rows.Add(rowInsert);

 

          }

}

catch(Exception e)

{

 

// Error Logging

 

}

finally

{

// Will always be called

 if (sr != null) // This check can be ignored though

                sr.Close();

}               

 

We started looking for a class (Helper Class), which can import/export flat files into DataTable/Array.

Then came across this useful dll FileHelpers. We converted our previous code to the one written below

 

Step 1) Created a class in .cs file and specify deliminator , which was | (pipe) in our case

[DelimitedRecord("|")]  

 public class Anxeure  

 {  

     public string COMP_NAME;  

       

     public string FULL_NAME;  

  

     public string FATHER_NAME;  

  

     public string DESIGNATION;  

    

     //..... and other columns in the sequence they will come in file.

  

 }  

 

 

 Step 2) Create an instance of FileHelperEngine and read the file.

 

 FileHelperEngine engine = new FileHelperEngine(typeof(Anxeure));  

  

 // To Read Use:  

 Anxeure[] res = engine.ReadFile("FileIn.txt") as Anxeure[];  

 

//We could also export the data thus created after doing some manipulation

engine.WriteFile("FileOut.txt", res);  

 

It is amazing how by writing two statements we could achieve a strongly typed output. Great

 

Hope it helps

Jatinder

 

 

Tuesday, May 19, 2009

Tip to optimize C# Refactoring in Web Projects

I usally do the refactoring of the web application code developed over period of time to make it more managable. But most of the time VS take too long that it becomes better to do refactoring on your own.Later I decided to read some blog posts to see whether someone has work on Optimising Refactor. After googling around for a while, I found the solution.

Scott has posted a great tip for speeding up refactoring performance with Web Projects in VS 2005.

Solution:

  1. Click Start->Run in Windows and run "regedit"
  2. Navigate to this registry location: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\CSharp\Options\Editor
  3. Right-click on the Editor node and choose "New DWORD value"
  4. Name the value "OpenAllVenusFilesOnRefactor" and leave the value as 0

Then, when you restart VS 2005 and perform a re-factoring - you should find performance very fast.

 

This hack basically turns off the refactoring functionality beyond the current page and so it's pretty fast. It looks like this has been fixed in Visual Studio 2008 thankfully.


Hope it Helps
Jatinder

Monday, May 11, 2009

JavaScript : Convert Ok/Cancel into Yes/No

Just came across a post, brilliant way to change the confirm box from ok n cancel to yes n no

 

Found the info on the link http://www.delphifaq.com/faq/javascript/f1172.shtml

 

Here is the code:

 

<script language=javascript>

 

/*@cc_on @*/

/*@if (@_win32 && @_jscript_version>=5)

 

function window.confirm(str)

{

    execScript('n = msgbox("'+str+'","4132")', "vbscript");

    return(n == 6);

}

 

@end @*/

</script>

 

Hope visitors find it useful.