Click here to Skip to main content
15,169,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public override void CreateNewOutputRows()
{
StreamReader sr = new StreamReader(fullFilePath);

int lineIndex = 0;
string line;
while (!sr.EndOfStream)
{
line = sr.ReadLine();

if (line.Substring(0,1).ToString() == "P")
{
HeaderBuffer.AddRow();
HeaderBuffer.Indicator = line.Substring(0,1).ToString();
}

lineIndex = lineIndex + 1;

}
sr.Close();
}

What I have tried:

I just need to read the header line 1st character from a flat file and write to table.
Please help why am I getting this error "Index and length must refer to a location within the string. Parameter name: length ".

Thanks
Posted
Updated 6-Jul-18 15:01pm
Comments
j snooze 6-Jul-18 17:07pm
   
pretty sure if you debug this and look at the value in your "line" variable its probably blank. So substring function yelling at you saying the index of 0 and length of 1 doesn't exist. You'll only find that out by debugging.
Jk19 7-Jul-18 23:44pm
   
Thanks for the quick reply Snooze!

You could have easily found this if you just ran the code under the debugger.

Your code is treating every line of the text file you're reading as if it had ONE OR MORE characters in it. That's just not the case. You've got blank lines in your file that have a length of 0, hence, the exception you're getting.

Now, what are you going to do about it? How about checking for a length of 0 before you do anything with the line?
   
v2
Comments
Jk19 7-Jul-18 23:43pm
   
Thanks Dave! will debug and check the error.
Quote:
why am I getting this error "Index and length must refer to a location within the string. Parameter name: length ".

The error message tells you that you try to substring outside of the string.
In
C#
line.Substring(0,1).ToString()

you forgot to check that line contain at least 1 char.
With the debugger, you can watch the program as it perform.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
Jk19 7-Jul-18 23:43pm
   
Thank you. I will follow your suggestions. I am new to VS(c#)
I have a Visual studio in which the debugger was not properly installed and it throws error when I try to run the script. I am trying to run the plugin to make it work..
Patrice T 9-Jul-18 15:37pm
   
reinstalling VS properly is worth the hassle.
Jk19 9-Jul-18 15:24pm
   
Reply


Also can you please advice to overcome the issue that I am getting on references..
"'HeaderBuffer' does not contain a definition for 'Version_Release' and no extension method 'Version_Release' accepting a first argument of type 'HeaderBuffer' could be found
(are you missing a using directive or an assembly reference?)

I am trying to pull in more columns and load the table.
if (line.Substring(0,1).ToString() == "P")
{
HeaderBuffer.AddRow();
HeaderBuffer.Indicator = line.Substring(0,1).ToString();
HeaderBuffer.Version_Release = line.Substring(2, 2).ToString();
}
..
Patrice T 9-Jul-18 15:43pm
   
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900