lucycopp
lucycopp

Reputation: 213

Saving keeps overwriting itself C#

I am making an application which will save and load products. These products have three properties of a product name, customer name and firmware location. However, when I try to save them, it will only save one and keeps overwriting with the most recent product saved. The following is my code for the product class:

  public class Product
{

    //private product data
    private string productName;

    public string getProductName()
    {
        return this.productName;
    }

    public void setProductName (string inProductName)
    {
        this.productName = inProductName;
    }

    private string customerName;

    public string getCustomerName()
    {
        return this.customerName;
    }

    public void setCustomerName (string inCustomerName)
    {
        this.customerName = inCustomerName;
    }

    private string firmwareLocation;

    public string getFirmwareLocation()
    {
        return this.firmwareLocation;
    }

    public void setFirmwareLocation (string inFirmwareLocation)
    {
        this.firmwareLocation = inFirmwareLocation;
    }


    //constructor 
    public Product (string inProductName, string inCustomerName, string inFirmwareLocation)
    {
        productName = inProductName;
        customerName = inCustomerName;
        firmwareLocation = inFirmwareLocation;
    }


    //save method
    public void Save (System.IO.TextWriter textOut)
    {
        textOut.WriteLine(productName);
        textOut.WriteLine(customerName);
        textOut.WriteLine(firmwareLocation);
    }

    public bool Save (string filename)
    {
        System.IO.TextWriter textOut = null;
        try
        {
            textOut = new System.IO.StreamWriter(filename);
            Save(textOut);
        }
        catch
        {
            return false;
        }
        finally
        {
            if (textOut != null)
            {
                textOut.Close();
            }
        }
        return true;
    }

At the end is my save methods.

Here is the code for when the user presses the add product button:

private void Add_Click(object sender, RoutedEventArgs e)
    {
        //get input from user
        string inputCustomerName = customerNameTextBox.Text;
        string inputProductName = productNameTextBox.Text;
        string inputFirmwareLocation = firmwareTextBox.Text;

        try
        {
            Product newProduct = new Product(inputProductName, inputCustomerName, inputFirmwareLocation);
            newProduct.Save("products.txt");
            MessageBox.Show("Product added");
        }
        catch
        {
            MessageBox.Show("Product could not be added");
        }
    }

Upvotes: 0

Views: 105

Answers (1)

Bojan B
Bojan B

Reputation: 2111

You are not appending the text to your file, thats why it keeps overwriting the last entry over and over again.

Try to change your save method to:

public bool Save (string filename)
    {
        System.IO.TextWriter textOut = null;
        try
        {
            textOut = new System.IO.StreamWriter(filename, true);
            Save(textOut);
        }
        catch
        {
            return false;
        }
        finally
        {
            if (textOut != null)
            {
                textOut.Close();
            }
        }
        return true;
    }

Notice the "true" as the second parameter in the StreamWriter constructor. This tells the StreamWriter to append the new line.

Upvotes: 2

Related Questions