c# - When I update a record gives the following error "Binary stream '0 'does not Contain a valid BinaryHeader" -
using system; using system.collections.generic; using system.linq; using system.text; using system.io; using system.runtime.serialization; using system.runtime.serialization.formatters.binary; namespace question {
this class
[serializable] public class employee { public int32 id { get; set; } public string name { get; set; } public string lastname { get; set; } }
this method write record
public class write { public void writerecord(employee emp) { if (!file.exists("records.dat")) { filestream savefile = new filestream("records.dat", filemode.openorcreate, fileaccess.write); binaryformatter formatter = new binaryformatter(); formatter.serialize(savefile, emp); savefile.flush(); savefile.close(); savefile.dispose(); formatter = null; } else { filestream savefile = new filestream("records.dat", filemode.append, fileaccess.write); binaryformatter formatter = new binaryformatter(); formatter.serialize(savefile, emp); savefile.flush(); savefile.close(); savefile.dispose(); formatter = null; } }
this method update
public void updaterecord(employee employ,int32 id) { binaryformatter formatter = new binaryformatter(); long record = 0; // filestream readfile = new filestream("records.dat", filemode.open, fileaccess.readwrite); while (record < readfile.length) { record = readfile.position; readfile.seek(record, seekorigin.begin); employ = (employee)formatter.deserialize(readfile); if (employ.id == id) { formatter.serialize(readfile, employ); readfile.flush(); readfile.close(); readfile.dispose(); formatter = null; return; } } }
when call method go error after update
public void readrecord() { binaryformatter formatter = new binaryformatter(); long record = 0; employee emp = new employee(); filestream readfile = new filestream("records.dat", filemode.open, fileaccess.readwrite); while (record < readfile.length) { readfile.seek(record, seekorigin.begin); emp = (employee)formatter.deserialize(readfile); console.writeline("number: " + emp.id); console.writeline("name: " + emp.name); console.writeline("last name: " + emp.lastname); console.writeline("====================================="); record = readfile.position; } readfile.flush(); readfile.close(); readfile.dispose(); formatter = null; }
this method return record
public void getrecord(ref employee emp, int id) { binaryformatter formatter = new binaryformatter(); long record = 0; employee emplea = new employee(); filestream readfile = new filestream("records.dat", filemode.open, fileaccess.readwrite); while (record < readfile.length) { readfile.seek(record, seekorigin.begin); emplea = (employee)formatter.deserialize(readfile); if (emplea.id == id) { emp = emplea; readfile.flush(); readfile.close(); readfile.dispose(); formatter = null; return; } record = readfile.position; } } } }
this mai class using system; using system.collections.generic; using system.linq; using system.text;
namespace question { class program { static void main(string[] args) { employee exaple1 = new employee(); exaple1.id = 1; exaple1.name = "juan"; exaple1.lastname = "perez"; write record = new write(); record.writerecord(exaple1); employee exaple2= new employee(); exaple2.id = 2; exaple2.name = "jose"; exaple2.lastname = "medina"; record.writerecord(exaple2); employee exaple3 = new employee(); exaple3.id = 3; exaple3.name = "marcos"; exaple3.lastname = "paniagua"; record.writerecord(exaple3); employee exaple4 = new employee(); exaple4.id = 4; exaple4.name = "nurys"; exaple4.lastname = "santnaa"; employee exaple5 = new employee(); exaple4.id = 5; exaple4.name = "carlos"; exaple4.lastname = "encarnacion"; record.writerecord(exaple5); console.writeline("records add"); console.readkey(); exaple1.id = 2; exaple1.name = "juan ramon"; exaple1.lastname = "gomez diaz"; record.updaterecord(exaple1, 2); console.writeline("record updated..."); console.readkey(); record.readrecord(); console.writeline("record reads..."); console.readkey(); employee emplea = new employee(); record.getrecord(ref emplea, 4); console.writeline("number: " + emplea.id); console.writeline("number: " + emplea.name); console.writeline("number: " + emplea.lastname); console.writeline("get record"); console.readkey(); } } }
Comments
Post a Comment