PC Review


Reply
Thread Tools Rate Thread

Compare to previous line

 
 
gwenna@gmail.com
Guest
Posts: n/a
 
      24th Jul 2006
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

}}

 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      24th Jul 2006
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;
}
}


--
Happy coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
gwenna
Guest
Posts: n/a
 
      24th Jul 2006
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

Morten Wennevik wrote:
> 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;
> }
> }
>
>
> --
> Happy coding!
> Morten Wennevik [C# MVP]


 
Reply With Quote
 
Adam Clauss
Guest
Posts: n/a
 
      24th Jul 2006
"gwenna" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...

> 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?

--
Adam Clauss


 
Reply With Quote
 
gwenna
Guest
Posts: n/a
 
      24th Jul 2006
Yes, I think that would work, but I don't know how to do that...

Adam Clauss wrote:
> "gwenna" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> > 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?
>
> --
> Adam Clauss


 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      24th Jul 2006
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;



On Mon, 24 Jul 2006 19:45:47 +0200, gwenna <(E-Mail Removed)> wrote:

> Yes, I think that would work, but I don't know how to do that...
>
> Adam Clauss wrote:
>> "gwenna" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>>
>> > 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?
>>
>> --
>> Adam Clauss

>
>




--
Happy coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
gwenna
Guest
Posts: n/a
 
      24th Jul 2006
Ah, I see, thank you very much!

Morten Wennevik wrote:
> 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;
>
>
>
> On Mon, 24 Jul 2006 19:45:47 +0200, gwenna <(E-Mail Removed)> wrote:
>
> > Yes, I think that would work, but I don't know how to do that...
> >
> > Adam Clauss wrote:
> >> "gwenna" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> >>
> >> > 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?
> >>
> >> --
> >> Adam Clauss

> >
> >

>
>
>
> --
> Happy coding!
> Morten Wennevik [C# MVP]


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ON SUBTOTAL LINE CARRY FORWARD DENOMINATOR FROM PREVIOUS LINE Pam M Microsoft Excel Worksheet Functions 1 22nd Jan 2009 07:15 AM
Compare to Previous Record hlock Microsoft Access Queries 3 11th Dec 2008 02:40 AM
Compare previous records in a query Jasper Recto Microsoft Access Queries 7 22nd Jul 2008 05:46 PM
Macro problem on, Yellowed line - previous line or next line. =?Utf-8?B?RWQ=?= Microsoft Excel Programming 8 29th Mar 2005 11:55 PM
Compare value to previous record CLR Microsoft Access Queries 7 16th Jan 2005 03:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:38 PM.