You're working on objects in memory... That's the reason you loose the data stored in a list or datatable.
There's few ways to store data...
1. Files:
- text file (*.txt),
- XML,
- etc.
2. A'ka database files
- MS Excel file,
- MS Access database,
3. Databases
- MS SQL Server,
- SQLite,
- Oracle,
- PostgreSQL,
- etc.
If you work on DataTable, you can simply save your data by using
DataTable.WriteXml[
^] method. Tobe able to read data from Xml, use
DataTable.ReadXml[
^] method.
Note, that DataTable has set of columns. When you set
AutoIncrement[
^] property for one of them, the number of order (
OrderId
) will be increased automatically.
Example:
DataColumn col1 = new DataColumn("AutoNumber", typeof(int));
col1.AutoIncrement = true;
col1.AutoIncrementSeed = 1;
col1.AutoIncrementStep = 1;
DataColumn col2 = new DataColumn("Name", typeof(string));
DataColumn col3 = new DataColumn("Age", typeof(int));
DataTable dt = new DataTable("table");
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.Rows.Add(new object[]{null, "Damian", 22});
dt.Rows.Add(new object[]{null, "Rose", 20});
dt.Rows.Add(new object[]{null, "Angelica", 19});
dt.Rows.Add(new object[]{null, "Romeo", 21});
string myxml = @"D:\students.xml";
dt.WriteXml(myxml);
dt.Rows.Clear();
dt.ReadXml(myxml);
Result:
AutoNumber Name Age
1 Damian 22
2 Rose 20
3 Angelica 19
4 Romeo 21
Good luck!
For further details, please, read these articles:
Save and restore your form size and location[
^]
Where should I store my data?[
^]