INTRODUCTION
Adding information to a data file of records is one of the most basic and needed functions in any organization. Not only does it keep everything in one central location, but it should also make it easy for someone to retrieve and change needed information quickly and easily. Here, I will describe
C# coding I have created that demonstrates different features I use to manage a simple customer data file in binary text format.
HOW IT WORKS The programming code I use works with a binary data file I call “customer.txt". The “currdir” variable I use contains the path of the current directory. It is concatenated to the beginning of the customer file to form the string expression, "currdir + customer.txt" . The defined constant, “CUSTOMERLEN”, is the length in characters of each customer record in the binary data file. To view the complete code readout, please click here.
After clicking the “Save” button in the screen, the fields (4 in this example) will first be retrieved under the function, “AddCustomerButton_Click(object sender, System.EventArgs e)”. The data will be written to the customer data file in one of two ways. When the selection flag variable “SelectVar” equals 0, this means no customer data record has yet been retrieved and the information will be appended to the customer data file. If “SelectVar” equals 1, then a customer record has been retrieved and the currently displayed record will be updated with the changes made by the user to the customer data file. Then the list box that displays all of the customer records in the data file will be refreshed.
Next, I will discuss customer record retrieval into the Winform. Each customer record in the binary file has been automatically assigned a unique number denoting its physical sequence in the data file. This is done during adding and deleting customer records. Whenever the customer data file is restructured, this sequential record numbering system will also be updated.
Upon retrieving the desired customer record into the string variable I call “CustomerListBox_String”, the program will next calculate its file position offset by using the record length constant of “CUSTOMERLEN”. This constant is then multiplied by the selected customer record’s physical sequence number to yield its file position offset, “choose_record_datafile_offset”. Now, this will be used to read the selected customer record from a file stream to "currdir + customer.txt". The record from the file is read into the same string variable from above, “CustomerListBox_String”. This is then selectively parsed and read into its counterpart text fields on the Winform. Below you see the code I used for this operation via the function, “CustomerListBox_SelectedIndexChanged(object sender, System.EventArgs e)”.
- private void CustomerListBox_SelectedIndexChanged(object sender, System.EventArgs e)
-
- {
-
-
-
- int[] convert_to_number = new int[5];
-
- string CustomerListBox_String;
-
-
-
-
-
- if (CustomerListBox.SelectedIndex != -1)
-
- {
-
-
-
- CustomerListBox_String = CustomerListBox.Text;
-
-
-
-
-
-
-
- for (a = 0; a < 5; a++)
-
- {
-
- convert_to_number[a] = 0;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "0") convert_to_number[a] = 0;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "1") convert_to_number[a] = 1;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "2") convert_to_number[a] = 2;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "3") convert_to_number[a] = 3;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "4") convert_to_number[a] = 4;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "5") convert_to_number[a] = 5;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "6") convert_to_number[a] = 6;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "7") convert_to_number[a] = 7;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "8") convert_to_number[a] = 8;
-
- if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "9") convert_to_number[a] = 9;
-
- }
-
- choose_record_datafile_offset = ((convert_to_number[0] * 10000) + (convert_to_number[1] * 1000) + (convert_to_number[2] * 100) + (convert_to_number[3] * 10) + (convert_to_number[4] * 1)) * CUSTOMERLEN;
-
-
-
-
-
- if (CustomerListBox_String.Substring(0, 4) == "Cust") MessageBox.Show("Invalid selection...", "Error Mode");
-
-
-
- if (CustomerListBox_String.Substring(0, 4) != "Cust")
-
- {
-
-
-
- SelectVar = 1;
-
-
-
-
-
-
-
-
-
- StreamReader streamobj = new StreamReader(currdir + "customer.txt");
-
- streamobj.BaseStream.Seek(choose_record_datafile_offset, SeekOrigin.Begin);
-
- CustomerListBox_String = streamobj.ReadLine();
-
- streamobj.Close();
-
-
-
-
-
-
-
-
-
- this.textBox1.Text = CustomerListBox_String.Substring(0, 35);
-
- this.textBox2.Text = CustomerListBox_String.Substring(35, 12);
-
- this.textBox3.Text = CustomerListBox_String.Substring(47, 8);
-
- this.textBox4.Text = CustomerListBox_String.Substring(55, 35);
-
- Cursor.Current = Cursors.Default;
-
- }
-
- }
Removing unwanted records is a necessary function of any Winform. When the user clicks the “Delete” button, the Winform function “DeleteCustomerButton_Click(object sender, System.EventArgs e)” is fired. This actually works in much the same way as the “CustomerListBox_SelectedIndexChanged(object sender, System.EventArgs e)” function discussed above. After the “remove_record_datafile_offset” file offset has been calculated, the program code simply rewrites the "currdir + customer.txt" file without the customer record that had been selected from the list box for removal. Then the list box holding all the customer records will be refreshed using the same coding as with the add/update feature mentioned above.
CONCLUSION
Differing variations of the programming code I have discussed here have been used in a number of my application development projects and have proven themselves to be very reliable. The last thing you want is to have your time wasted when you are filling customer orders and trying to get things shipped out. I prefer to work with binary text files as a software development professional, because I can repair them easily and quickly in the event of a problem.