true;
ArrayList ar =
new
ArrayList();using (StreamReader
sr = new
StreamReader(@"C:\Inetpub\wwwroot\Final Year Project\Project\Website_GreenICT\All.txt"))
{
string line;
if ((line = sr.ReadLine()) !=
null)
{
ar.Add(line);
}
while (((line = sr.ReadLine()) !=
null))
{
DateTime dt;if (DateTime.TryParse(line,
out dt))
{
if (dt.ToShortDateString() ==
Convert.ToDateTime(TextBox1.Text).ToShortDateString())
{
if ((line = sr.ReadLine()) !=
null)
{
ar.Add(line);
}
}
}
}
}
if (ar.Count > 0)
ar.RemoveAt(ar.Count - 1);
//remove last
if (ar.Count == 0)
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"Alert",
"alert('No Records Found')",
true);
foreach (string s
in ar)Label1.Text += s +
Environment.NewLine;
}
it for me to find a particular record whereby user will state a date on a textbox and the values that corresponds to the date will be replied. The values inside my textfile is like that:
36
4/23/2009 3:21:22 PM
36
4/23/2009 3:22:12 PM
35
4/23/2009 3:22:53 PM
36
4/23/2009 3:23:22 PM
35
4/23/2009 3:28:27 PM
37
4/23/2009 4:38:01 PM
37
4/23/2009 4:44:16 PM
37
4/23/2009 4:44:44 PM
37
4/23/2009 4:45:30 PM
37
4/23/2009 4:46:39 PM
37
4/23/2009 4:46:54 PM
37
4/23/2009 4:47:36 PM
36
4/23/2009 4:47:46 PM
30
4/24/2009 10:13:20 AM
30
4/24/2009 10:13:41 AM
31
4/24/2009 10:27:14 AM
31
4/24/2009 10:27:26 AM
26
4/27/2009 10:20:30 AM
28
4/27/2009 11:22:07 AM
28
4/27/2009 11:22:23 AM
28
4/28/2009 9:45:55 AM
28
4/28/2009 9:46:11 AM
28
4/28/2009 9:46:27 AM
47
5/14/2009 3:24:27 PM
47
5/14/2009 3:24:40 PM
for date 4/27/2009, 4/28/2009 i can find all the records that correspond to the date but for 5/14/2009 i can only get the value 36 which is not even related to 5/14/2009, may i know how to solve this error? Please help!
ArrayList ar = new ArrayList();
using (StreamReader sr = new StreamReader(@"C:\All.txt"))
{
string line;
string LastLine = "";
while (((line = sr.ReadLine()) != null))
{
DateTime dt;
if (DateTime.TryParse(line, out dt))
{
if (dt.ToShortDateString() == Convert.ToDateTime(TextBox1.Text).ToShortDateString())
ar.Add(LastLine);
}
LastLine = line;
}
}
if (ar.Count == 0)
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('No Records Found')", true);
foreach (string s in ar)
Label1.Text += s + Environment.NewLine;
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by Figo Fei - MSFT on May 19, 2009 02:54 AM
Read the first line of the file and save it in ar (this is where you get the 36 from)
For each line
If the line is a date and it matches TextBox1 then store the value from the
next line into ar.
Remove the last entry from ar
So in the case of 5/14/2009 your code puts 36 into ar (the first line of the file) and then it puts 47 (in particular the 47 which comes
after the first entry for 5/14/2009, ie the second last line of the file) into ar. It then finds another 5/14/2009 but there is nothing after it in the file so no more entries
are made in ar. At this point ar has 36 and 47. Your final step is to remove the last entry and so you get 36.
What do you want the program to do? You say you want to find all records that correspond to the date but are they the records that come before or the records that come after the matching date? Why do you remove the last entry from ar? Is that by design?
jingjie28
Member
99 Points
219 Posts
Looping Problem???
May 14, 2009 08:17 AM|LINK
Hi everyone i have this coding.
Label1.Text = "";Label2.Visible =
true; Label2.Visible = false;Button4.Visible=
true; ArrayList ar = new ArrayList();using (StreamReader sr = new StreamReader(@"C:\Inetpub\wwwroot\Final Year Project\Project\Website_GreenICT\All.txt")){
string line; if ((line = sr.ReadLine()) != null){
ar.Add(line);
}
while (((line = sr.ReadLine()) != null)){
DateTime dt;if (DateTime.TryParse(line, out dt)){
if (dt.ToShortDateString() == Convert.ToDateTime(TextBox1.Text).ToShortDateString()){
if ((line = sr.ReadLine()) != null){
ar.Add(line);
}
}
}
}
}
if (ar.Count > 0)ar.RemoveAt(ar.Count - 1);
//remove last if (ar.Count == 0)Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "Alert", "alert('No Records Found')", true); foreach (string s in ar)Label1.Text += s + Environment.NewLine;}
it for me to find a particular record whereby user will state a date on a textbox and the values that corresponds to the date will be replied. The values inside my textfile is like that:
36
4/23/2009 3:21:22 PM
36
4/23/2009 3:22:12 PM
35
4/23/2009 3:22:53 PM
36
4/23/2009 3:23:22 PM
35
4/23/2009 3:28:27 PM
37
4/23/2009 4:38:01 PM
37
4/23/2009 4:44:16 PM
37
4/23/2009 4:44:44 PM
37
4/23/2009 4:45:30 PM
37
4/23/2009 4:46:39 PM
37
4/23/2009 4:46:54 PM
37
4/23/2009 4:47:36 PM
36
4/23/2009 4:47:46 PM
30
4/24/2009 10:13:20 AM
30
4/24/2009 10:13:41 AM
31
4/24/2009 10:27:14 AM
31
4/24/2009 10:27:26 AM
26
4/27/2009 10:20:30 AM
28
4/27/2009 11:22:07 AM
28
4/27/2009 11:22:23 AM
28
4/28/2009 9:45:55 AM
28
4/28/2009 9:46:11 AM
28
4/28/2009 9:46:27 AM
47
5/14/2009 3:24:27 PM
47
5/14/2009 3:24:40 PM
for date 4/27/2009, 4/28/2009 i can find all the records that correspond to the date but for 5/14/2009 i can only get the value 36 which is not even related to 5/14/2009, may i know how to solve this error? Please help!
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Looping Problem???
May 14, 2009 12:44 PM|LINK
ArrayList ar = new ArrayList(); using (StreamReader sr = new StreamReader(@"C:\All.txt")) { string line; string LastLine = ""; while (((line = sr.ReadLine()) != null)) { DateTime dt; if (DateTime.TryParse(line, out dt)) { if (dt.ToShortDateString() == Convert.ToDateTime(TextBox1.Text).ToShortDateString()) ar.Add(LastLine); } LastLine = line; } } if (ar.Count == 0) Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('No Records Found')", true); foreach (string s in ar) Label1.Text += s + Environment.NewLine;Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Paul Linton
Star
13431 Points
2535 Posts
Re: Looping Problem???
May 15, 2009 01:03 AM|LINK
My reading of your code is as follows
Read the first line of the file and save it in ar (this is where you get the 36 from)
For each line
If the line is a date and it matches TextBox1 then store the value from the next line into ar.
Remove the last entry from ar
So in the case of 5/14/2009 your code puts 36 into ar (the first line of the file) and then it puts 47 (in particular the 47 which comes after the first entry for 5/14/2009, ie the second last line of the file) into ar. It then finds another 5/14/2009 but there is nothing after it in the file so no more entries are made in ar. At this point ar has 36 and 47. Your final step is to remove the last entry and so you get 36.
What do you want the program to do? You say you want to find all records that correspond to the date but are they the records that come before or the records that come after the matching date? Why do you remove the last entry from ar? Is that by design?