Reading next block of data on button click event
-
Hi Guys and ladies. I am reading a record into a windows form which works fine. The record structure is: Header details of 463 chars and then a repetative set of records of 276 chars each. Problem is that the string can have anything between 1 and a max of 50 repetative records. On clicking the next button i am trying to display the details from the second record etc. I am trying to figure out how to best acieve this as if I click the button the first time it reads the second block of data fine, if I click it again it reads the second block again instead of reading the third bloack of data. How do I read the data to be able to get to the third record? Would it be best to load the blocks into a dataset and then maybe read that? Below is the code I currently have.
private void exitToolStripMenuItem\_Click(object sender, EventArgs e) { this.Dispose(); this.Close(); } private void button2\_Click(object sender, EventArgs e) { filepath = txtFileName.Text; StreamReader sr = new StreamReader(filepath); string holdLine = sr.ReadLine(); int recLength = holdLine.Length; int startIndex = 463; int oneRecLength = 276; int totalRecs = (recLength - startIndex) / oneRecLength; int startread = 739; int lastread = 0; if ((recLength - 463) > 1015) { if((totalRecs - 1) != 0) { string nextrec = holdLine.Substring(startread, 276); HOM\_CO\_ID = nextrec.Substring(0, 5); //= 5 HOM\_PROD\_CODE = nextrec.Substring(5, 3); //= 3 HOM\_ACCT\_TYPE = nextrec.Substring(8, 1); //= 1 HOM\_BRANCH = nextrec.Substring(9, 6); //= 6 HOM\_ACCOUNT\_NO = nextrec.Substring(15, 23); //= 23 HOM\_BUDGET\_NO = nextrec.Substring(38, 5); //= 5 HOM\_ACCOUNT\_NAME = nextrec.Substring(43, 30); //= 30 HOM\_AMOUNT = nextrec.Substring(73, 17); //= 17 HOM\_REF = nextrec.Substring(90, 20); //= 20 HOM\_DESCR = nextrec.Substring(110, 47); //= 47 HOM\_TRAN\_CODE = nextrec.Substring(157, 5); //= 5 HOM\_REVERSAL\_DESCR = nextrec.Substring(162, 47); //= 47 NOM\_REF2 = nextrec.Substring(209, 20); //= 20 NOM\_DESCRIPTION = nextrec.S
-
Hi Guys and ladies. I am reading a record into a windows form which works fine. The record structure is: Header details of 463 chars and then a repetative set of records of 276 chars each. Problem is that the string can have anything between 1 and a max of 50 repetative records. On clicking the next button i am trying to display the details from the second record etc. I am trying to figure out how to best acieve this as if I click the button the first time it reads the second block of data fine, if I click it again it reads the second block again instead of reading the third bloack of data. How do I read the data to be able to get to the third record? Would it be best to load the blocks into a dataset and then maybe read that? Below is the code I currently have.
private void exitToolStripMenuItem\_Click(object sender, EventArgs e) { this.Dispose(); this.Close(); } private void button2\_Click(object sender, EventArgs e) { filepath = txtFileName.Text; StreamReader sr = new StreamReader(filepath); string holdLine = sr.ReadLine(); int recLength = holdLine.Length; int startIndex = 463; int oneRecLength = 276; int totalRecs = (recLength - startIndex) / oneRecLength; int startread = 739; int lastread = 0; if ((recLength - 463) > 1015) { if((totalRecs - 1) != 0) { string nextrec = holdLine.Substring(startread, 276); HOM\_CO\_ID = nextrec.Substring(0, 5); //= 5 HOM\_PROD\_CODE = nextrec.Substring(5, 3); //= 3 HOM\_ACCT\_TYPE = nextrec.Substring(8, 1); //= 1 HOM\_BRANCH = nextrec.Substring(9, 6); //= 6 HOM\_ACCOUNT\_NO = nextrec.Substring(15, 23); //= 23 HOM\_BUDGET\_NO = nextrec.Substring(38, 5); //= 5 HOM\_ACCOUNT\_NAME = nextrec.Substring(43, 30); //= 30 HOM\_AMOUNT = nextrec.Substring(73, 17); //= 17 HOM\_REF = nextrec.Substring(90, 20); //= 20 HOM\_DESCR = nextrec.Substring(110, 47); //= 47 HOM\_TRAN\_CODE = nextrec.Substring(157, 5); //= 5 HOM\_REVERSAL\_DESCR = nextrec.Substring(162, 47); //= 47 NOM\_REF2 = nextrec.Substring(209, 20); //= 20 NOM\_DESCRIPTION = nextrec.S
I'd create two class members (class variables) of type -
List<String>
records -int
recordIndex In the button2_Click I'd test to see if records collection is null; If null I'd read the text from file and add each record as a string to the records collection. I'd then reference the string in the records collection at the recordIndex. Extract the values from the string, set the text control values and increment to recordIndex by one. You might want to dispose of that Streamreader as well, once finished. sr.Dispose();"You get that on the big jobs."
-
Hi Guys and ladies. I am reading a record into a windows form which works fine. The record structure is: Header details of 463 chars and then a repetative set of records of 276 chars each. Problem is that the string can have anything between 1 and a max of 50 repetative records. On clicking the next button i am trying to display the details from the second record etc. I am trying to figure out how to best acieve this as if I click the button the first time it reads the second block of data fine, if I click it again it reads the second block again instead of reading the third bloack of data. How do I read the data to be able to get to the third record? Would it be best to load the blocks into a dataset and then maybe read that? Below is the code I currently have.
private void exitToolStripMenuItem\_Click(object sender, EventArgs e) { this.Dispose(); this.Close(); } private void button2\_Click(object sender, EventArgs e) { filepath = txtFileName.Text; StreamReader sr = new StreamReader(filepath); string holdLine = sr.ReadLine(); int recLength = holdLine.Length; int startIndex = 463; int oneRecLength = 276; int totalRecs = (recLength - startIndex) / oneRecLength; int startread = 739; int lastread = 0; if ((recLength - 463) > 1015) { if((totalRecs - 1) != 0) { string nextrec = holdLine.Substring(startread, 276); HOM\_CO\_ID = nextrec.Substring(0, 5); //= 5 HOM\_PROD\_CODE = nextrec.Substring(5, 3); //= 3 HOM\_ACCT\_TYPE = nextrec.Substring(8, 1); //= 1 HOM\_BRANCH = nextrec.Substring(9, 6); //= 6 HOM\_ACCOUNT\_NO = nextrec.Substring(15, 23); //= 23 HOM\_BUDGET\_NO = nextrec.Substring(38, 5); //= 5 HOM\_ACCOUNT\_NAME = nextrec.Substring(43, 30); //= 30 HOM\_AMOUNT = nextrec.Substring(73, 17); //= 17 HOM\_REF = nextrec.Substring(90, 20); //= 20 HOM\_DESCR = nextrec.Substring(110, 47); //= 47 HOM\_TRAN\_CODE = nextrec.Substring(157, 5); //= 5 HOM\_REVERSAL\_DESCR = nextrec.Substring(162, 47); //= 47 NOM\_REF2 = nextrec.Substring(209, 20); //= 20 NOM\_DESCRIPTION = nextrec.S
Well, it seems to me that this line
int startIndex = 463;
is where the problem lies. You should make startIndex a class level variable, then when you finished reading a record you increment this by 276. Personally, I would create a separate class that holds a complete transaction, and then read the file only once, populating a List of such transactions as I went along. Then you can just navigate through the list, backwards and forwards as you wish.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
Hi Guys and ladies. I am reading a record into a windows form which works fine. The record structure is: Header details of 463 chars and then a repetative set of records of 276 chars each. Problem is that the string can have anything between 1 and a max of 50 repetative records. On clicking the next button i am trying to display the details from the second record etc. I am trying to figure out how to best acieve this as if I click the button the first time it reads the second block of data fine, if I click it again it reads the second block again instead of reading the third bloack of data. How do I read the data to be able to get to the third record? Would it be best to load the blocks into a dataset and then maybe read that? Below is the code I currently have.
private void exitToolStripMenuItem\_Click(object sender, EventArgs e) { this.Dispose(); this.Close(); } private void button2\_Click(object sender, EventArgs e) { filepath = txtFileName.Text; StreamReader sr = new StreamReader(filepath); string holdLine = sr.ReadLine(); int recLength = holdLine.Length; int startIndex = 463; int oneRecLength = 276; int totalRecs = (recLength - startIndex) / oneRecLength; int startread = 739; int lastread = 0; if ((recLength - 463) > 1015) { if((totalRecs - 1) != 0) { string nextrec = holdLine.Substring(startread, 276); HOM\_CO\_ID = nextrec.Substring(0, 5); //= 5 HOM\_PROD\_CODE = nextrec.Substring(5, 3); //= 3 HOM\_ACCT\_TYPE = nextrec.Substring(8, 1); //= 1 HOM\_BRANCH = nextrec.Substring(9, 6); //= 6 HOM\_ACCOUNT\_NO = nextrec.Substring(15, 23); //= 23 HOM\_BUDGET\_NO = nextrec.Substring(38, 5); //= 5 HOM\_ACCOUNT\_NAME = nextrec.Substring(43, 30); //= 30 HOM\_AMOUNT = nextrec.Substring(73, 17); //= 17 HOM\_REF = nextrec.Substring(90, 20); //= 20 HOM\_DESCR = nextrec.Substring(110, 47); //= 47 HOM\_TRAN\_CODE = nextrec.Substring(157, 5); //= 5 HOM\_REVERSAL\_DESCR = nextrec.Substring(162, 47); //= 47 NOM\_REF2 = nextrec.Substring(209, 20); //= 20 NOM\_DESCRIPTION = nextrec.S
If it's only 50 records you should parse and read the entire file into a List<Record>, where a Record is a data holding class which is populated in a similar way to your code here, at the start. Working with a proper domain object with properties is much better than working with a string format the whole time. To read all the records you do something like
List<Record> GetRecords(string s){
List<Record> r = new List<Record>();
ReadHeader(s.Substring(0, HEADER_LENGTH));
s = s.Substring(HEADER_LENGTH);
while(s.Length > 0){
string recordstr = s.Substring(0, RECORD_LENGTH);
s = s.Substring(RECORD_LENGTH);Record rec = new Record();
rec.CO_ID = recordstr.Substring(0, 5); //= 5
// etcr.Add(rec);
}
return r;
}ed: HEADER_LENGTH = 463 and RECORD_LENGTH = 276.