Click here to Skip to main content
15,917,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this program get some info from user and save it in a text file but when we want to see all saved records the last on just shown


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace repair
{

   

    public partial class Form1 : Form
    {
        private FileStream output;
        private FileStream input;
        private BinaryFormatter formtter = new BinaryFormatter();
        BinaryFormatter reader = new BinaryFormatter();
        public Form1()
        {
            InitializeComponent();
        }
        private void openfile()
        {
            output = new FileStream("C:\\Users\\lenovo\\Documents\\kar.txt",FileMode.Append,FileAccess.Write);
        }
        public void cleartextbox()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openfile();
            user User=new user();
            try
            {
                User.Name = textBox1.Text;
                User.Family = textBox2.Text;
                User.Job = textBox3.Text;
            }
            catch(FormatException)
            {
                MessageBox.Show("invaid format");
                output.Close();
                return;
            }
            formtter.Serialize(output, User);
            output.Close();
            cleartextbox();
        }

        private void button2_Click(object sender, EventArgs e)
        {
try
{
    input = new FileStream("C:\\Users\\lenovo\\Documents\\kar.txt", FileMode.Open, FileAccess.Read);
    textBox4.Text = "users are:";
    while (true)
    {
        user User = (user)reader.Deserialize(input);
        string result = User.Name+"\t" + User.Family+"\t" + User.Job+"\t";
        textBox4.Text ="\r\n" +result;
    }
}
            catch(FileNotFoundException)
{
    MessageBox.Show("file noot exists");
    return;
}
            catch(SerializationException)
{
    input.Close();
}
        }
        
    }
}



and this is the user class:


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace repair
{
    [Serializable]
    class user
    {
        private string name;
        private string family;
        private string job;
        public user()
        {
        name=" ";
        family = " ";
        job = " ";


    }
        public user( string namevalue,string familyvalue,string jobvalue)
        {
            name = namevalue;
            family = familyvalue;
            job = jobvalue;
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
            public string Family
            {
                get
                {
                    return family;
                }
                set
                {
                    family=value;
                }

            }
        public string Job
        {
            get
            {
                return job;
            }
            set
            {
                job=value;
            }
        }
        }
    }
Posted
Comments
sachin4dotnet 10-Apr-15 1:48am    
What is your problem? Is there any error or wrong output or which output you want?? give some explanation...
sachin4dotnet 10-Apr-15 1:52am    
If you are having a problem of getting only last record then probably you are not appending string. you should use :
textBox4.Text += "\r\n" +result;
Akbar Fardi 10-Apr-15 7:17am    
if i want to remove some part of stored record in file how can i?

1 solution

use stringbuilder as below
C#
StringBuilder builder = new StringBuilder();
builder.Append("users are:");
builder.AppendLine
while (true)
{
    user User = (user)reader.Deserialize(input);
    builder.AppendLine(User.Name+"\t" + User.Family+"\t" + User.Job+"\t");
}
textBox4.Text= builder.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900