none
Problems with Data Saving RRS feed

  • Question

  • Hi everyone. I have partially created a system in my program which allows for clicking on an event button to slide up a box. It has textboxes so you can write down data.

    Here is the Program ID:RLM785

    My question is: why does saving data get overwritten by previously saved data if I don't intend it to?

    I have programmed it so this doesn't happen but it still does. Not sure why and I want to get it fixed. I've tried fixing it but I'm getting no where.

    To see what I want occurring, do the following:

    1. save the smallbasic program somewhere first or else it won't save the file
    2. save data for event 1 (anything really) then
    3. save data for event 2.

    Now, where you save the smallbasic program find a file named gcf.rmr. Open it with notepad and see the result.

    Now, to see the problem, do exactly as said above for step 1 to 3 but this time also save data for event 3. Open the gcf again see the result. Event 2 data has been overwritten by Event 3 data. I can't explain it.

    Thanks for taking your time in reading my problem above.


    Sincerely, Anthony :)


    • Edited by Tony93 Saturday, March 24, 2012 2:28 AM
    Saturday, March 24, 2012 2:26 AM

Answers

  • Wow, my head is spinning. I've taken the courage to study your code to find out where it is failing at.
    You should really try to put those animations and other heavy code in separate subroutines. It makes your code much easier to analyze and modify.
    As I've figured out, after attributing last button clicked in button_clicked, you create a For...Loop using m=1 up to m=4.
    Then you start a huge nested cluster of If and ElseIfs. For this debugging, I'll just analyze the ElseIf btn = button_clicked Then block, which is executed when Save button (btn) is selected.
    At this point, you create another For...Loop i=1 to i=k (w/ k=5), representing the 5 fields of an "Event" form.
    Till now all is running as intended; but when we reach the line... -> File.WriteLine(Calendar_Storage,(k*m)+i,Notes[m][name])
    Well, the formula used to decide which line to save ain't right -> k*m + i
    Let's see why:
    w/ m=1, i=1 and k=5 we have: 5*1 + 1 = 5 + 1 = 6
    As you can see, the 1st line you try to save in gcf.rmr is Line 6, up to Line 10!
    If you've read what I had explained above about the File.WriteLine()'s anomaly, you know even if it tries to save at the 6th line, it saves at the 1st line if gcf.rmr were empty.
    Then, after that bugged line code, you issue -> Controls.SetTextBoxText(box[i],"")
    It means that from now on, whatever was in that typed TextBox field is no more!
    What is the consequence of this? Well, when the code goes back to the next For...Loop iteration, w/ m=2, m=3 and m=4; it just saves empty lines ("") up to line #25.
    In short, your code always tries to save starting from Line #6 up to Line #10, overwriting anything already in there, and it doesn't matter which "Event" form used either!!! And of course, the extra blank lines till #26.
    I hope this helps ya correcting your program. :D
    Saturday, March 24, 2012 8:19 AM
    Answerer
  • I confess I don't have much experience w/ the File object of Small Basic. But some other day, I made some experiments w/ File.WriteLine(); and unfortunately, it wasn't how I'd expected to be. =(
    Here's the code more or less I've used to test so:

    Path = Program.Directory + "\MyLines.txt"
    Line = 10
    File.WriteLine(Path,Line  "Line: " + Line)

    What happens is, if the target file doesn't have the 10th line, it doesn't automatically create enough lines to reach that. Instead, it just writes in at the last line available. But if the last line has anything on it, then it creates a new line before writing in it.
    Let's suppose MyLines.txt has 6 lines and the last one is empty. After running the code above, and opening the file, you'll see "Line: 10" saved at the 6th line!
    In the case the last line wasn't empty, you'll see "Line: 10" written at the 7th line instead.
    A curious thing I've also noticed: A file always has an empty line at the end of it after a File.WriteLine().

    Saturday, March 24, 2012 5:19 AM
    Answerer

All replies

  • I confess I don't have much experience w/ the File object of Small Basic. But some other day, I made some experiments w/ File.WriteLine(); and unfortunately, it wasn't how I'd expected to be. =(
    Here's the code more or less I've used to test so:

    Path = Program.Directory + "\MyLines.txt"
    Line = 10
    File.WriteLine(Path,Line  "Line: " + Line)

    What happens is, if the target file doesn't have the 10th line, it doesn't automatically create enough lines to reach that. Instead, it just writes in at the last line available. But if the last line has anything on it, then it creates a new line before writing in it.
    Let's suppose MyLines.txt has 6 lines and the last one is empty. After running the code above, and opening the file, you'll see "Line: 10" saved at the 6th line!
    In the case the last line wasn't empty, you'll see "Line: 10" written at the 7th line instead.
    A curious thing I've also noticed: A file always has an empty line at the end of it after a File.WriteLine().

    Saturday, March 24, 2012 5:19 AM
    Answerer
  • After briefly analyzing your program; especially the For i=1 To k loop; I've concluded you need to save 5 lines for each of your 4 "Event" forms.
    That is, if you need to save the 3rd "Event", they'll go into lines #11 up to #15, which is (3event x 5lines - 4) right?
    Although I've never coded anything like it, I've pictured some procedures needed for it!

    1. You'll have to code a subroutine which reads the contents of target "database" file, and then, count how many lines it's got.
    2. If it is less than it's necessary to save an "Event" form (in the above case, having less than 11 lines), append that many empty lines at the end of it to reach the desired number.
    3. Finally use File.WriteLine(Calendar_Storage,(k*m)+i,Notes[m][name]); now assured it's gonna write in the exact line numbers you intended them to be! :D

    Good luck coding it!!!    ^_^

    Saturday, March 24, 2012 6:13 AM
    Answerer
  • Thanks for the replies, but I think this is going to take a lot of thinking aye. If anyone has any more suggestions or help on this topic, it would be much appreciated, thanks.

    Sincerely, Anthony :)

    Saturday, March 24, 2012 7:42 AM
  • Wow, my head is spinning. I've taken the courage to study your code to find out where it is failing at.
    You should really try to put those animations and other heavy code in separate subroutines. It makes your code much easier to analyze and modify.
    As I've figured out, after attributing last button clicked in button_clicked, you create a For...Loop using m=1 up to m=4.
    Then you start a huge nested cluster of If and ElseIfs. For this debugging, I'll just analyze the ElseIf btn = button_clicked Then block, which is executed when Save button (btn) is selected.
    At this point, you create another For...Loop i=1 to i=k (w/ k=5), representing the 5 fields of an "Event" form.
    Till now all is running as intended; but when we reach the line... -> File.WriteLine(Calendar_Storage,(k*m)+i,Notes[m][name])
    Well, the formula used to decide which line to save ain't right -> k*m + i
    Let's see why:
    w/ m=1, i=1 and k=5 we have: 5*1 + 1 = 5 + 1 = 6
    As you can see, the 1st line you try to save in gcf.rmr is Line 6, up to Line 10!
    If you've read what I had explained above about the File.WriteLine()'s anomaly, you know even if it tries to save at the 6th line, it saves at the 1st line if gcf.rmr were empty.
    Then, after that bugged line code, you issue -> Controls.SetTextBoxText(box[i],"")
    It means that from now on, whatever was in that typed TextBox field is no more!
    What is the consequence of this? Well, when the code goes back to the next For...Loop iteration, w/ m=2, m=3 and m=4; it just saves empty lines ("") up to line #25.
    In short, your code always tries to save starting from Line #6 up to Line #10, overwriting anything already in there, and it doesn't matter which "Event" form used either!!! And of course, the extra blank lines till #26.
    I hope this helps ya correcting your program. :D
    Saturday, March 24, 2012 8:19 AM
    Answerer
  • Try this.  RLM785-0    

    Using file.appendcontents,  file data is saved like this . (csv)  

       Name,25,Mar,2012,abcdefg........

    Saturday, March 24, 2012 8:48 AM
    Answerer