0

I'm trying to update my file every time I add a new price to a certain ID.

I have a .csv file containing multiple rows such as ID, Name, Address, State, Zip, Age, Ordered. All Ordered fields start off as 0.

Right now, I have a form where you can select the ID and insert a price....I want that price to be added onto the Total Ordered in the .csv.

So essentially, if the Ordered starts at 0 and I add a price of 11.45..the new .csv should reflect 11.45 in the Ordered row.. Then, if I open the form again and add another price of 2.00...the new Ordered would be 13.45.

    public void Customer()
    {
        string name = "";
        string address = "";
        string state = "";
        int customerID = 0;
        int zip = 0;
        int age = 0;
        double Ordered = 0;

        Customer[] Array = Customer.getAll();

        for (int k = 0; k < Array.Length; k++)
        {
            if (Array[k] != null)
            {
                ID = Array[k].getID();
                if (Convert.ToInt32(Field.Text) == ID)
                {
                    ID = Array[k].getID();
                    name = Array[k].getName();
                    address = myCustArray[k].getAddress();
                    state = myCustArray[k].getState();
                    zip = myCustArray[k].getZip();
                    age = myCustArray[k].getAge();
                    Ordered = (Array[k].getOrdered()
                    + Convert.ToDouble(PriceField.Text));

                    using (FileStream fs = new FileStream(@"customer.csv",FileMode.Append, FileAccess.Write))
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(customerID + "," + name + "," + address + "," + state + "," + zip + "," + age + "," + totalOrdered + "/r/n");
                    }
                }
            }
        }
    }
}

}

HNVDB
  • 39
  • 1
  • 7
  • I'd just do what Andrew said below and use your array to rewrite the file: http://stackoverflow.com/a/7405989/771928 . Use the create as it will overwrite your current file. Be sure to close your readers and writers too! – KreepN Dec 13 '12 at 06:36

1 Answers1

1

The only way to do it is to rewrite the file with the new data.

Theoretically you could open the file for writing, seek to the position of the value you want to change, and then write the new value, but that new value would have to be the same size (in terms of number of characters) as the one it's replacing because you can't expand or shrink the space available for it in the file. And you also have the hassle of parsing the file to find the right place to put the value.

Update

Writing the data out as a comlete line, as you suggest, would be a simpler way to do it. you'd need to do the following though.

  1. Open the source file for reading
  2. Create a new file for writing
  3. Read lines from the source file and write them to the target file until you find the line you want to change
  4. Write the new line using the data in your array, instead of the line in the source file
  5. Continue copying lines from the source to target.
  6. Close both files
  7. delete the source file
  8. Rename the target file to replace the source file.

Or, if your array contains all the data in the CSV you can just dump it out to a new CSV and then do 7 and 8 above.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Edited my post, can I save the entire line which has the selected customer ID in the drop down? (Name, Address, State, Zip, Age, TotalOrdered) – HNVDB Dec 13 '12 at 03:29
  • Same answer. Only if the line is exactly the same number of characters as the one it's replacing. – Andrew Cooper Dec 13 '12 at 03:32
  • My array contains all the data in the CSV, what would that code look like, or do you know of any resources I could look at? I'm relatively new to c# – HNVDB Dec 13 '12 at 03:47
  • Simplest would be to create a method on your `Customer` object called `ToCSVRecord` (or something similar) which returns a string. You could then just create a new file and iterate through your array calling `ToCSVRecord` on each object to write the file. – Andrew Cooper Dec 13 '12 at 03:56