Link Unit
Sunday, July 05, 2009
System.IO.DirectoryNotFoundException: Could not find a part of the path
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:
- Click Start->Run in Windows and run "regedit"
- Navigate to this registry location: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\CSharp\Options\Editor
- Right-click on the Editor node and choose "New DWORD value"
- 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
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>
Tuesday, April 28, 2009
How to Solve Method Error 500 - Maximum Length Exceeded with AJAX web service call
At client side I've been assigned a bug concerning
a combo box. When the combo box is changed by the
user, a secondary combo box has its values reloaded.
This is done by calling a web service. The bug is
that "Method Error 500" is displayed as the only
item on occasion. I checked and cross checked the
problem with my own post
http://jatindersingh.blogspot.com/2008/03/method-error-12030.html ,
but could not sort the error , search on google lead to
the following solution. Solution:After some rooting around, it turns out that this is
caused by too much data being returned in the SQL query
behind the web service call, so the web service returns
an error. A simple change to the web.config file can
increase the size limit of data returned: <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="5000000" /> </webServices> </scripting></system.web.extensions> Explaination:First off, the serialization process will fail if the
number of nested objects is greater than that defined
within the RecursionLimit property , which is 100.
The serialization process will also fail if the length
of the serialized text is greater than that of the
MaxJsonLength property; again, note the default value
of 2,097,152 base ten.Source http://www.codeproject.com/KB/ajax/ajax_json_serialization.aspx
Hope visitor find it useful.
Wednesday, April 15, 2009
string.Empty Example and Issues in C# written by Sam Allen
string.Empty Example and Issues in C# written by Sam Allen
Problem. You are wondering how to use string.Empty and how it is different from an empty string literal constant "". There is a subtle different, and it can be significant in some scenarios, in that it changes the meaning of your program. Solution. Here we see how to use string.Empty, and then look into its implementation, finally seeing an example of how it can produce less effective code.
Tuesday, April 14, 2009
Crystal Reports Toolbar Images Displaying as Red X
Solution:
Copy aspnet_client folder from your PC and paste it in application folder on server ,then refresh the IIS default website and its done. It solved the problem for me I hope it help visitors too.