473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Compare to previous line

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

}}

Jul 24 '06 #1
6 1994
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]
Jul 24 '06 #2
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]
Jul 24 '06 #3
"gwenna" <gw****@gmail.comwrote in message news:11*********************@s13g2000cwa.googlegro ups.com...
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
Jul 24 '06 #4
Yes, I think that would work, but I don't know how to do that...

Adam Clauss wrote:
"gwenna" <gw****@gmail.comwrote in message news:11*********************@s13g2000cwa.googlegro ups.com...
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
Jul 24 '06 #5
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 <gw****@gmail.comwrote:
Yes, I think that would work, but I don't know how to do that...

Adam Clauss wrote:
>"gwenna" <gw****@gmail.comwrote in message news:11*********************@s13g2000cwa.googlegro ups.com...
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]
Jul 24 '06 #6
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 <gw****@gmail.comwrote:
Yes, I think that would work, but I don't know how to do that...

Adam Clauss wrote:
"gwenna" <gw****@gmail.comwrote in message news:11*********************@s13g2000cwa.googlegro ups.com...

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]
Jul 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
5
by: PyPK | last post by:
I have two files file1 in format <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T F file2 same as file1 <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T T
2
by: SP | last post by:
Hi All, I need to compare two files line by line and copy the differing lines to a new files. I.e. Compare file1 and file2 line by line. Copy only differing lines to file3. I tried a couple...
2
by: deancarstens | last post by:
Hi, I'm a novice with VB and I've written this pretty simple script, but for the life of me, I can't get the last bit of the script to work. It should compare the value entered by the user...
13
by: Shelley | last post by:
Compare Current Year Worksheet with Previous Year Worksheet and if SSN exists in Current Year Worksheet & Not in Previous Year - Copy this Row from Current Year Worksheet & Paste into Previous Year...
1
by: Neilen Marais | last post by:
Hi I'm trying to compare some text to find differences other than whitespace. I seem to be misunderstanding something, since I can't even get a basic example to work: In : d =...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
1
omerbutt
by: omerbutt | last post by:
hi there i am making a page where customer can edit his current order, the tools i am using are mysql and php i am using 2 tables for this 1.rage_uk_bulk_order 2.rage_uk_bulk_order_products i...
25
by: Brian | last post by:
I have a datetimepicker formated for just time, the user selects the time. I want to compare if that time is between midnight and 8 am dtmTime #11:59:59 PM# and dtmTime < #08:00:00 AM# this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.