I have isolated the issue down to manually removing 1 line in the report source code. When this line is removed the report imports successfully.
The line that makes the importer properly function is:
(DOES WORK)
Recently that line has changed to:
Recently that line has changed to:
(DOESNT WORK)
The error message received when importing the DOESNT WORK line is:Object reference not set to an instance of an object.
I have uploaded the section of code that handles the importer click
to import function
Any help would be greatly appreciated.
Thanks
The error message received when importing the DOESNT WORK line is:Object reference not set to an instance of an object.
I have uploaded the section of code that handles the importer click
to import function
Any help would be greatly appreciated.
Thanks
13 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jan 30, 2012, 7:15 PM
It's only the using statement for the StreamWriter that needs altering as I set out in my previous post.
The idea is to close the file before you open it again with this line:
ImportResult importResult = handler.Import(destinationDocPath,this.fldDefaultRoundSpin.Value.ToString());
srod srodPosted Jan 31, 2012, 6:24 PM
tmpLine = tmpLine.Replace("
tmpLine = tmpLine.Replace("
I couldnt get it to work without adding these in addition to following your sugesstions for the StreamWriter function.
I also had to add 2 if statement in the StremWriter statement as follows:
if (items[i].ToString() != "
{
if (items[i].ToString() != "
{
{
writer.WriteLine(items[i].ToString());
}
// writer.Write(lineToWrite.ToString());
}
Your help is very much appreciated. :)
srod srodPosted Jan 30, 2012, 6:20 PM
What I changed from your suggestion is in red:
private void btnImportFile_Click(object sender, ImageClickEventArgs e)
{
try
{
IImportModule handler = this.GetImportModule();
string documentPath = this.Server.MapPath(this.ResolveUrl(this.UploadFile.PathLocation));
string destinationDocPath = documentPath;
if (handler.GetType().FullName == "MySite.ReportImporter.MyCreditReportType")
{
destinationDocPath = documentPath.Replace(".htm", "_r.htm");
System.Collections.ArrayList items = new System.Collections.ArrayList();
string tmpLine;
//READ
string search = "
string replacement = "
using (StreamReader reader = new StreamReader(documentPath))
using (StreamWriter writer = new StreamWriter(destinationDocPath))
{
//MODIFY LINE BY LINE WHILE READING
while ((tmpLine = reader.ReadLine()) != null)
{
while ((tmpLine = reader.ReadLine()) != null)
{
tmpLine = tmpLine.Replace(search, replacement);
items.Add(tmpLine);
}
for (int i = 0; i < items.Count; i++)
{
writer.WriteLine(items[i].ToString());
}
}
}
}
ImportResult importResult = handler.Import(destinationDocPath,this.fldDefaultRoundSpin.Value.ToString());
if (importResult.Success)
{
this.ImportAccounts = importResult.Item;
this.CurrentPage = 0;
this.BindReviewAccounts();
this.mv.SetActiveView(this.vwReviewImport);
if (!CommonUtility.IsNullOrEmpty(importResult.FailedRecords))
{
this.grdRecordErrors.DataSource = importResult.FailedRecords;
this.grdRecordErrors.DataBind();
}
}
else
{
this.SetError(importResult.FriendlyMessage);
}
}
catch (Exception ex)
{
SiteLog.LogError(ex);
this.SetError("An error occurred importing your file. Please ensure the file is complete and try again.");
}
}
VulpesPosted Jan 30, 2012, 2:21 PM
}
srod srodPosted Jan 29, 2012, 2:14 PM
using (StreamWriter writer = new StreamWriter(destinationDocPath))
{
for (int i = 0; i < items.Count; i++)
{
{
{
writer.WriteLine(items[i].ToString());
}
}
ImportResult importResult = handler.Import(destinationDocPath, this.fldDefaultRoundSpin.Value.ToString() );
if (importResult.Success)
{
this.ImportAccounts = importResult.Item;
this.CurrentPage = 0;
this.BindReviewAccounts();
this.mv.SetActiveView(this.vwReviewImport);
if (!CommonUtility.IsNullOrEmpty(importResult.FailedRecords))
{
this.grdRecordErrors.DataSource = importResult.FailedRecords;
this.grdRecordErrors.DataBind();
}
}
else
{
this.SetError(importResult.FriendlyMessage);
}
}
}
}
catch (Exception ex)
{
SiteLog.LogError(ex);
this.SetError("An error occurred importing your file. Please ensure the file is complete and try again.");
}
}
Thanks!
VulpesPosted Jan 29, 2012, 8:52 AM
The error message suggests that you've opened that file in another program, perhaps in an HTML viewer or editor.
However, from the code you've posted, I can't see the end of the second 'using' statement, so it's possible that you might be doing something in there - before the file is closed - that is causing the error.
Sam HobbsPosted Jan 28, 2012, 11:25 PM
Using the DOM might make your code immune to minor changes such as you are plagued with.
Note that the reason I am not saying more is that you obviously have a lot of time invested in the solution you have and I assume you are not inerested in doing it another way.
srod srodPosted Jan 28, 2012, 6:27 PM
Method Name: WinIOError
Declaring Type: System.IO.__Error
Message:
The process cannot access the file 'C:\Directory\Site\Files\Uploads\uniquecustid\SAMPLE REPORT_r.htm' because it is being used by another process.
Could this be because the importer is already running the following function? destinationDocPath = documentPath.Replace(".htm", "_r.htm");thanks
VulpesPosted Jan 28, 2012, 6:06 PM
System.Collections.ArrayList items = new System.Collections.ArrayList();
string tmpLine;
//READ
string search = "
string replacement = "
using (StreamReader reader = new StreamReader(documentPath))
{
//MODIFY LINE BY LINE WHILE READING
while ((tmpLine = reader.ReadLine()) != null)
{
tmpLine = tmpLine.Replace(search, replacement);
items.Add(tmpLine);
}
}
//WRITE
using (StreamWriter writer = new StreamWriter(destinationDocPath))
{
for (int i = 0; i < items.Count; i++)
{
writer.WriteLine(items[i].ToString());
}
}
srod srodPosted Jan 28, 2012, 5:51 PM
The new line of source that the credit report provider added to their reports is
I am trying to have the reader look for
and then replace it with either the line
or it could just remove the line that doesn't work,
The importer will work with
thanks
VulpesPosted Jan 28, 2012, 5:35 PM
Having removed these strings before adding them to 'items', I didn't then understand why you were double-checking that they weren't there before writing the relevant lines to the destination file?
srod srodPosted Jan 28, 2012, 4:40 PM
private void btnImportFile_Click(object sender, ImageClickEventArgs e)
{
try
{
IImportModule handler = this.GetImportModule();
string documentPath = this.Server.MapPath(this.ResolveUrl(this.UploadFile.PathLocation));
//modified 8/3/2011
string destinationDocPath = documentPath;
//DO THIS ONLY IF TRUECREDIT
if (handler.GetType().FullName == "MySite.ReportImporter.ReportModel")
{
//modified 8/1/2011 - modify source document prior to load
destinationDocPath = documentPath.Replace(".htm", "_r.htm");
//StringBuilder lineToWrite = new StringBuilder();
System.Collections.ArrayList items = new System.Collections.ArrayList();
string tmpLine;
int numLines = 0;
//READ
using (StreamReader reader = new StreamReader(documentPath))
{
//MODIFY LINE BY LINE WHILE READING
while ((tmpLine = reader.ReadLine()) != null)
{
tmpLine.Replace("
tmpLine.Replace("
//lineToWrite.Append(tmpLine);
items.Add(tmpLine);
numLines++;
}
}
int finalLines = numLines;
//WRITE
using (StreamWriter writer = new StreamWriter(destinationDocPath))
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ToString() != "
{
if (items[i].ToString() != "
{
{
writer.WriteLine(items[i].ToString());
}
// writer.Write(lineToWrite.ToString());
}
}
VulpesPosted Jan 28, 2012, 8:49 AM
You've omitted to upload the code so we can have a look at this.