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
VulpesRank #55 · 29.2K reputationPosted Jan 30, 2012, 2:15 PM EST
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());
VulpesRank #55 · 29.2K reputationPosted Jan 28, 2012, 3:49 AM EST
You've omitted to upload the code so we can have a look at this.
srod srod14 reputationPosted Jan 28, 2012, 11:40 AM EST
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());
}
}
VulpesRank #55 · 29.2K reputationPosted Jan 28, 2012, 12:35 PM EST
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 srod14 reputationPosted Jan 28, 2012, 12:51 PM EST
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
VulpesRank #55 · 29.2K reputationPosted Jan 28, 2012, 1:06 PM EST
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 srod14 reputationPosted Jan 28, 2012, 1:27 PM EST
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
Sam HobbsRank #107 · 20.5K reputationPosted Jan 28, 2012, 6:25 PM EST
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.
VulpesRank #55 · 29.2K reputationPosted Jan 29, 2012, 3:52 AM EST
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.
srod srod14 reputationPosted Jan 29, 2012, 9:14 AM EST
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!
VulpesRank #55 · 29.2K reputationPosted Jan 30, 2012, 9:21 AM EST
}
srod srod14 reputationPosted Jan 30, 2012, 1:20 PM EST
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.");
}
}
srod srod14 reputationPosted Jan 31, 2012, 1:24 PM EST
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. :)
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts