1

I have created a WinForms note app using C# and VS2015, in which the user can write notes and save it temporally while the form is opened. But once the user has closed the app and re-opened it, his saved notes will disappear. How can I save the notes for the next open so that he can see, edit and read them again? Here's my code:

private void Form1_Load(object sender, EventArgs e)
{
    table = new DataTable();
    table.Columns.Add("Title", typeof(string));
    table.Columns.Add("Message", typeof(string));
    dataGridView1.DataSource = table;
    dataGridView1.Columns["Message"].Visible = false;
    dataGridView1.Columns["Title"].Width = 140;
}

private void btnNew_Click(object sender, EventArgs e)
{
    textBox1.Clear();
    textBox2.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
    table.Rows.Add(textBox1.Text, textBox2.Text);
    textBox1.Clear();
    textBox2.Clear();
}

private void btnRead_Click(object sender, EventArgs e)
{
    int index = dataGridView1.CurrentCell.RowIndex;
    if (index > -1)
    {
        textBox1.Text = table.Rows[index].ItemArray[0].ToString();
        textBox2.Text = table.Rows[index].ItemArray[1].ToString();
    }
}

private void btnDelete_Click(object sender, EventArgs e)
{
    int index = dataGridView1.CurrentCell.RowIndex;
    table.Rows[index].Delete();
}

Here's my form image:

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Ayman
  • 23
  • 9
  • 2
    Save them as text files, or in a database. – stuartd Sep 06 '16 at 15:11
  • 1
    If you don't have a database, you can use [`WriteXml`](https://msdn.microsoft.com/en-us/library/3hyx9hb4(v=vs.110).aspx) method of your data table to save it to a file. Then to load it again you can use [`ReadXml`](https://msdn.microsoft.com/en-us/library/fs0z9zxd(v=vs.110).aspx) method. – Reza Aghaei Sep 06 '16 at 15:15
  • @RezaAghaei Can I have a code example please ? – Ayman Sep 06 '16 at 15:25
  • Do you want the user to always have the last text? Or be able to save with a specific name and then open a specific file, just like Notepad? – Andrew Sep 06 '16 at 15:26
  • No. Sorry. Look up DataTable.WriteXML() – Shannon Holsinger Sep 06 '16 at 15:26
  • @Ayman, if you need something that simple, please do a minimal research. There are thousands of examples in Stack Overflow, the MSDN and the rest of the web. For example: http://stackoverflow.com/questions/1919552/how-to-write-a-text-file-in-c-sharp and https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx. – Andrew Sep 06 '16 at 15:28
  • Possible duplicate of [C# Write Data to a text File](http://stackoverflow.com/questions/23395491/c-sharp-write-data-to-a-text-file) – Manfred Radlwimmer Sep 06 '16 at 15:30
  • @Ayman the `ReadXml` method contains an example for both save and load. – Reza Aghaei Sep 06 '16 at 15:32

2 Answers2

1

You can use your Application Settings to store some data. The default scope is "User" (stored by and for the current user only) but can also be "Application" (available for every user of your app). You can access that data through Properties.Settings.Default.myCustomSetting;

DKH
  • 452
  • 7
  • 15
0

The reason is that during execution of the program the image of the process is in the main memory (RAM) and when the program is finished, its pages are thrown out of the main memory and its data part is not going to save any where (its current data). When you run your program again the program became to process (comes from hard to main memory) and it loads from the first and does not have any knowledge about previous sessions unless your program itself refers to somewhere in hard disk and use the predefined data.
There are so many ways to do so, I say some of them:
1) Create and use a database which has the same criteria as your table here.
2) Make binary or text file using methods available in System.IO namespace which stores the content in file system (in your hard drive). When your project starts up, load the content from the stored file.
3) Use serialization which has different approaches in C#, for example use System.Xml.Serialization namespace.

Green Falcon
  • 818
  • 3
  • 17
  • 48
  • 1
    Actually I have to mention that there is also another simple way, use RichTextBox and use its Save method which can save content as .rtf extension. – Green Falcon Sep 06 '16 at 19:55