my WindowsApplication program has the following labels in my form named 'frmCustomerDetails'.
- lblCustomerID - displays the Customer ID (int value)
- lblCompanyName - string value
- lblAddress - string value
- lblContactPerson - string value
- lblContactNo - string value (or is it an int value)
In a windows application program, I should able to display my records coming from a .txt file. Using the StreamReader, I should able to display my records to the given labels.
I have a mp.txt file found in C:
here's a sample input inside the mp.txt
12345#John#Ohio#Jake#Arizona#
wherein '#' is the delimeter
I should able to display my records separately using the Split method.
However, my codes doesn't run correctly:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
private void frmCustomerDetails_Load(object sender, EventArgs e)
{
StreamReader reader = new StreamReader(@"C:\mp2.txt");
string line;
string[] records;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
records = line.Split("#");
records[0] = long.Parse(txtCustomerID.Text);
records[1] = txtContactPerson.Text;
records[2] = txtAddress.Text;
records[3] = txtContactPerson;
records[4] = txtContactNo;
}
reader.Close();
Any way to fix these codes?
AlanPosted Dec 12, 2007, 3:32 PM
To cycle through the records, you'll have to save them all to an array first and so the code is going to need to be changed quite a bit. Something like this (off top of my head so untested):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
// add private fields
private string[] lines;
private int nextLine = 0;
private void frmCustomerDetails_Load(object sender, EventArgs e)
{
// get all lines from file and store them in 'lines' array
lines = File.ReadAllLines(@"C:\mp2.txt");
// display first one
DisplayRecord(0);
}
private void DisplayRecord(int index)
{
string[] records = lines[index].Split('#');
txtCustomerID.Text = records[0];
txtContactPerson.Text = records[1];
txtAddress.Text = records[2];
txtContactPerson.Text = records[3];
txtContactNo.Text = records[4];
if (index < lines.Count - 1) nextLine = index + 1; // change nextLine
}
// add these eventhandlers in designer so they're automatically wired up
private void btnFirst_Click( object sender, EventArgs e)
{
DisplayRecord(0);
}
private void btnNext_Click( object sender, EventArgs e)
{
DisplayRecord(nextLine);
}
Mervha NifflemhellPosted Dec 12, 2007, 11:37 AM
Thank's a lot. That really worked.
Suppose I have three lines of records on my mp.txt:
123#A#qwe#Z#321#
456#B#asd#Y#654#
789#C#zxc#X#987#
And I have this buttons named ' btnFirst' and 'btnNext'
wherein btnFirst - displays the first list
btnNext - displays the next after the first
How can I able to put that in codes in the btnFirst and btnNext, along with the previous corrrect codes?
AlanPosted Dec 12, 2007, 10:51 AM
Try changing your while statement to this:
while (!reader.EndOfStream)
{
line = reader.ReadLine();
records = line.Split('#'); // single quotes here
// all these were the wrong way around!
// no need for any conversions - they're all strings already
txtCustomerID.Text = records[0];
txtContactPerson.Text = records[1];
txtAddress.Text = records[2];
txtContactPerson.Text = records[3];
txtContactNo.Text = records[4] ;
}