void Serializing() //writing suggest drug object in the file { Drugs drug = new Drugs(); drug._drugType = DrugTypeTb.Text; drug._drugName = DrugNameTb.Text; drug._drugStrenght = DrugStrenghtTb.Text; Stream stream = null; IFormatter formatter = new BinaryFormatter(); string path = First_Page.drug_path + "\\" + "Drug_Suggestions.txt"; try { if (File.Exists(path)) { stream = new FileStream(path, FileMode.Append, FileAccess.Write); } else { throw new CustomException("Cannot open file"); } } catch (CustomException e) { MessageBox.Show(e.Message, "Error"); } formatter.Serialize(stream, drug); stream.Close(); }
private void filereading() { Stream read = null; int i = 1; IFormatter formatter = new BinaryFormatter(); string path = drugs_path + "Drug_Suggestions.txt"; try { if (File.Exists(path)) { read = new FileStream(path, FileMode.Open, FileAccess.Read); } else { throw new CustomException("Cannot open Drugs.txt"); } } catch (CustomException e) { MessageBox.Show(e.Message); } while (read.Position != read.Length) { drug = (Drugs)formatter.Deserialize(read); AddRow(i); i += 1; } read.Close(); }
//Update Button private void button6_Click(object sender, EventArgs e) { Drugs d = new Drugs(); d._drugType = dataGridView1.Rows[option].Cells[1].Value.ToString(); d._drugName = dataGridView1.Rows[option].Cells[2].Value.ToString(); d._drugStrenght = dataGridView1.Rows[option].Cells[3].Value.ToString(); updatefiling(d, option + 1); txttb.Clear(); btndelete.Visible = false; btnupdate.Visible = false; disable_dataGridView(); }
void updatefiling(Drugs d, int count) { Drugs drug = new Drugs(); IFormatter formatter = new BinaryFormatter(); string path = drugs_path; Stream write = new FileStream(path + "temp.txt", FileMode.Create, FileAccess.Write); Stream read = null; try { if (File.Exists(path + "Drugs.txt")) { read = new FileStream(path + "Drugs.txt", FileMode.Open, FileAccess.Read); } else { throw new CustomException("Cannot open Drugs.txt"); } } catch (CustomException e) { MessageBox.Show(e.Message); } int i = 0; while (read.Position != read.Length) { i += 1; drug = (Drugs)formatter.Deserialize(read); if (i != count) { formatter.Serialize(write, drug); } else { formatter.Serialize(write, d); } } read.Close(); write.Close(); File.Delete(path + "Drugs.txt"); File.Move(path + "temp.txt", path + "Drugs.txt"); dataGridView1.Rows.Clear(); filereading(); }
void AddDrugs() //for adding drugs_names in the combo box { Drugs drug = null; IFormatter formatter = new BinaryFormatter(); string path = First_Page.drug_path + "\\" + "Drugs.txt"; Stream read = new FileStream(path, FileMode.Open, FileAccess.Read); while (read.Position != read.Length) { drug = (Drugs)formatter.Deserialize(read); comboDrug.Items.Add(drug._drugName); } read.Close(); }
//When admin wants to approve any row void Approve_drug(int count) { Stream read = null; IFormatter formatter = new BinaryFormatter(); string path = drugs_path + "Drug_Suggestions.txt"; try { if (File.Exists(path)) { read = new FileStream(path, FileMode.Open, FileAccess.Read); } else { throw new CustomException("Cannot open DrugSuggestions.txt"); } } catch (CustomException e) { MessageBox.Show(e.Message); } for (int i = 1; i <= count; ++i) { drug = (Drugs)formatter.Deserialize(read); } read.Close(); MessageBox.Show(drug._drugType); MessageBox.Show(drug._drugName); MessageBox.Show(drug._drugStrenght); path = drugs_path + "Drugs.txt"; Stream write = new FileStream(path, FileMode.Append, FileAccess.Write); formatter.Serialize(write, drug); write.Close(); RemoveSuggestion(count); }