Compare to previous line

G

gwenna

I have problem.

I am reading a text file using a streamreader and I want to check if a
field in the line I'm reading is the same as one in the line previously
read before doing something but .. when the line I'm reading is the
first line, I get an error... How can I check that the previous line
exists?

So far I have

using (StreamReader sr = new StreamReader(F))
while((line = sr.ReadLine())!= null)
{
checkPolicy = line.Substring(13,11);
if checkPolicy = polDS.Policy[0].POLICYNUM
{
// do this
}
else
{
// do that

}}
 
M

Morten Wennevik

Hi gwenna,

You aren't showing us how you keep track of the old line.

Anyhow, the below should give you what you need.

using (StreamReader sr = new StreamReader(F))
{
string line, oldline = null;
while ((line = sr.ReadLine()) != null)
{
if(line != oldline)
// new line, compute something
oldline = line;
}
}
 
G

gwenna

Hi Morten,

Thank you for responding. The old line was populated into a dataset.
One thing, though, I don't think I was clear, I need to check if a
"field" in the previous line is the same as the current "field" IF the
previous line exists.

the Text file would be something like
(the 9999999
field is the one I'm checking)

99044 Shirley Jones B12320040604 9999999
99044 John Jones B12320050604 9999999
99044 Fred Armstrong B12320040604 8888888

I read the line and parse it into a dataset, write it to a file then
read the next, append it to the file etc etc. When the number at
line.Substring(39,7) is the same as that same substring in the previous
line, I'm ADDING that person to the family rather than starting a new
family.

I can get everything to work EXCEPT that when it's the first line in
the text file, the dataset field doesn't yet exist...Hope I'm making
myself clear.

Gwenna
 
A

Adam Clauss

gwenna said:
I can get everything to work EXCEPT that when it's the first line in
the text file, the dataset field doesn't yet exist...Hope I'm making
myself clear.

Just check to see if there are any rows in your dataset?
 
M

Morten Wennevik

A DataSet can consist of many DataTables, although usually it is just 1.The rows are in the DataTable.

if(myDataSet.Tables[0].Rows.Count == 0)
continue;
 
G

gwenna

Ah, I see, thank you very much!

Morten said:
A DataSet can consist of many DataTables, although usually it is just 1. The rows are in the DataTable.

if(myDataSet.Tables[0].Rows.Count == 0)
continue;



Yes, I think that would work, but I don't know how to do that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top