Hi,
I have created a text file but the code will only read in the last line in the file. Can't see why this is. I can create new entrys into the text file but again I can only load the last line details.
Any help appreciated.
// Loads all records from file and add to ArrayList(recordList)
public void loadRecord()
{
TextReader tr = File.OpenText(@"C:\\acc.txt");
recordList = new ArrayList();
string name, line;
string address;
int pCode;
/*int accountno;
double balance;
string birthdate;
string accountname;*/
string[] data = null;
line = tr.ReadLine();
while (line != null) // reading a file line by line
{
data = line.Split(','); // splitting each record at every ','
name = data[0]; // after splitting at 1st index is name
address = data[1];
pCode = int.Parse(data[2]); // after splitting at 2nd index is pincode
accountno = int.Parse(data[3]);// after splitting at 3nd index is account number
balance = double.Parse(data[4]); // after splitting at 4rd index is balance
birthdate = (data[5]);
accountname = (data[6]);
Program r = new Program(name, address, pCode, accountno, balance, birthdate, accountname);
recordList.Add(r); // adding Record object to arrayList
line = tr.ReadLine();
}
tr.Close();
}
// Saving the record to file.
public void saveRecord()
{
TextWriter tw = File.CreateText(@"C:\\acc.txt");
for (int j = 0; j < recordList.Count; j++)
{
record = (Program)recordList[j];
tw.WriteLine(record.Name + "," + record.Address + "," + record.PinCode + "," + record.Accountno + "," + record.Balance + "," + record.birthdate + "," + record.accountname);
}
}
ArrayList recordList=null;
protected void Page_Load(object sender, EventArgs e)
{
loadRecord();
}
public void loadRecord()
{
TextReader tr = File.OpenText(@"E:\acc.txt");
recordList = new ArrayList();
string name, line;
string address;
int pCode;
int accountno;
double balance;
string birthdate;
string accountname;
string[] data = null;
line = tr.ReadLine();
while (line != null) // reading a file line by line
{
data = line.Split(','); // splitting each record at every ','
name = data[0]; // after splitting at 1st index is name
address = data[1];
pCode = int.Parse(data[2]); // after splitting at 2nd index is pincode
accountno = int.Parse(data[3]);// after splitting at 3nd index is account number
balance = double.Parse(data[4]); // after splitting at 4rd index is balance
birthdate = (data[5]);
accountname = (data[6]);
Program r = new Program();
r.name = data[0];
r.address = data[1];
r.pCode = data[2];
r.accountno = data[3];
r.balance = data[3];
r.birthdate = data[4];
r.accountname = data[5];
recordList.Add(r); // adding Record object to arrayList
line = tr.ReadLine();
}
tr.Close();
}
// Saving the record to file.
public void saveRecord(ArrayList recordList)
{
TextWriter tw = File.CreateText(@"C:\\acc.txt");
for (int j = 0; j < recordList.Count; j++)
{
Program record = (Program)recordList[j];
tw.WriteLine(record.name + "," + record.address + "," + record.pCode + "," + record.accountno + "," + record.balance + "," + record.birthdate + "," + record.accountname);
}
}
}
public class Program
{
public string name{get;set;}
public string address{get;set;}
public string pCode{get;set;}
public string accountno {get;set;}
public string balance{get;set;}
public string birthdate{get;set;}
public string accountname { get;set; }
}
But I am still gettint the same problem, It will only recognise the last entry into the text file. Displays incorrect pin if i enter anyone elses details.
Try This code from my Snippet its working perfect to read .txt file
// Open a file for reading
string FILENAME = Server.MapPath("MahadTECH.txt");
// Get a StreamReader class that can be used to read the file
StreamReader objStreamReader;
objStreamReader = File.OpenText(FILENAME);
// Now, read the entire file into a string
string contents = objStreamReader.ReadToEnd();
// Set the text of the file to a Web control
// We may wish to replace carraige returns with <br>s
MyLabel.Text = contents.Replace("\r\n", "<br>");
objStreamReader.Close();
Good luck`
Sincerely,
Mahad Bin Mukhtar Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
Hi, the following example can read text file content line by line, and display them on the page, you can refer to it:
protected void Page_Load(object sender, EventArgs e)
{
try
{
// "TestFile.txt" is under the root path of the web application.
using (StreamReader sr = new StreamReader(Server.MapPath("TestFile.txt")))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Response.Write(line + "<br />");
}
}
}
catch (Exception ex)
{
Response.Write("The file could not be read: <br />");
Response.Write(ex.Message);
}
}
macg
Member
1 Points
19 Posts
text file
Jul 10, 2012 05:07 PM|LINK
// Loads all records from file and add to ArrayList(recordList) public void loadRecord() { TextReader tr = File.OpenText(@"C:\\acc.txt"); recordList = new ArrayList(); string name, line; string address; int pCode; /*int accountno; double balance; string birthdate; string accountname;*/ string[] data = null; line = tr.ReadLine(); while (line != null) // reading a file line by line { data = line.Split(','); // splitting each record at every ',' name = data[0]; // after splitting at 1st index is name address = data[1]; pCode = int.Parse(data[2]); // after splitting at 2nd index is pincode accountno = int.Parse(data[3]);// after splitting at 3nd index is account number balance = double.Parse(data[4]); // after splitting at 4rd index is balance birthdate = (data[5]); accountname = (data[6]); Program r = new Program(name, address, pCode, accountno, balance, birthdate, accountname); recordList.Add(r); // adding Record object to arrayList line = tr.ReadLine(); } tr.Close(); } // Saving the record to file. public void saveRecord() { TextWriter tw = File.CreateText(@"C:\\acc.txt"); for (int j = 0; j < recordList.Count; j++) { record = (Program)recordList[j]; tw.WriteLine(record.Name + "," + record.Address + "," + record.PinCode + "," + record.Accountno + "," + record.Balance + "," + record.birthdate + "," + record.accountname); } }anand.csharp
Member
282 Points
76 Posts
Re: text file
Jul 10, 2012 05:55 PM|LINK
ArrayList recordList=null; protected void Page_Load(object sender, EventArgs e) { loadRecord(); } public void loadRecord() { TextReader tr = File.OpenText(@"E:\acc.txt"); recordList = new ArrayList(); string name, line; string address; int pCode; int accountno; double balance; string birthdate; string accountname; string[] data = null; line = tr.ReadLine(); while (line != null) // reading a file line by line { data = line.Split(','); // splitting each record at every ',' name = data[0]; // after splitting at 1st index is name address = data[1]; pCode = int.Parse(data[2]); // after splitting at 2nd index is pincode accountno = int.Parse(data[3]);// after splitting at 3nd index is account number balance = double.Parse(data[4]); // after splitting at 4rd index is balance birthdate = (data[5]); accountname = (data[6]); Program r = new Program(); r.name = data[0]; r.address = data[1]; r.pCode = data[2]; r.accountno = data[3]; r.balance = data[3]; r.birthdate = data[4]; r.accountname = data[5]; recordList.Add(r); // adding Record object to arrayList line = tr.ReadLine(); } tr.Close(); } // Saving the record to file. public void saveRecord(ArrayList recordList) { TextWriter tw = File.CreateText(@"C:\\acc.txt"); for (int j = 0; j < recordList.Count; j++) { Program record = (Program)recordList[j]; tw.WriteLine(record.name + "," + record.address + "," + record.pCode + "," + record.accountno + "," + record.balance + "," + record.birthdate + "," + record.accountname); } } } public class Program { public string name{get;set;} public string address{get;set;} public string pCode{get;set;} public string accountno {get;set;} public string balance{get;set;} public string birthdate{get;set;} public string accountname { get;set; } }Anand Kumar
ASP.NET Forum, Artilces, Blogs | www.CsharpMagic.com
macg
Member
1 Points
19 Posts
Re: text file
Jul 10, 2012 07:04 PM|LINK
Thanks anand.csharp,
But I am still gettint the same problem, It will only recognise the last entry into the text file. Displays incorrect pin if i enter anyone elses details.
MahadTECH
Star
8976 Points
1659 Posts
Re: text file
Jul 10, 2012 07:37 PM|LINK
Hello macg,
You are reffer to these links:
Try This code from my Snippet its working perfect to read .txt file
// Open a file for reading string FILENAME = Server.MapPath("MahadTECH.txt"); // Get a StreamReader class that can be used to read the file StreamReader objStreamReader; objStreamReader = File.OpenText(FILENAME); // Now, read the entire file into a string string contents = objStreamReader.ReadToEnd(); // Set the text of the file to a Web control // We may wish to replace carraige returns with <br>s MyLabel.Text = contents.Replace("\r\n", "<br>"); objStreamReader.Close();Good luck`
Mahad Bin Mukhtar
Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
Allen Li - M...
Star
10411 Points
1196 Posts
Re: text file
Jul 12, 2012 06:35 AM|LINK
Hi, the following example can read text file content line by line, and display them on the page, you can refer to it:
protected void Page_Load(object sender, EventArgs e) { try { // "TestFile.txt" is under the root path of the web application. using (StreamReader sr = new StreamReader(Server.MapPath("TestFile.txt"))) { String line; while ((line = sr.ReadLine()) != null) { Response.Write(line + "<br />"); } } } catch (Exception ex) { Response.Write("The file could not be read: <br />"); Response.Write(ex.Message); } }If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework