Click here to Skip to main content
15,893,368 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i created a list that creates number for me. The code to the list looks as follows
List<int> add_list = new List<int>();
    for (int i = 0; i < 400; i++)
    {
        add_list.Add(i);
    }


Next im taking each number and im assigning it to a sale order column in my DataTable Data table i created.

The problem im getting is when i close the app and open it again the list keeps starting from one and it shouldnt, it should start where it last stopped.

What I have tried:

I've tried to write every number in to a txt file on my desktop. It then reads the txt file and if the txt fil doesn't have the number in it starts from there.

Is there an easier way to do this then the way im doing it?
Posted
Updated 6-Sep-18 2:30am
Comments
CHill60 6-Sep-18 8:01am    
You need to store where you got up to somewhere ... how about a database? Most databases will actually do this for you with an "Identity Column".

The text file idea should work as well though ... or a config file ... or in the registry
Member 13475897 6-Sep-18 8:15am    
so i just create a table and put in one column as the self incrementing identity column? I would then have to read down every number in the table and show it in my data table. How wil i only take number that have not been used before. for example i'm creating orders then i need to asign an unique order number to each order. if i just read all those numbers from sql to my list how wil i make sure im not using a duplicate. if there is about 1000 records in my table there wil be 1000 numbers so there wil be 1000 numbers in my list. How do i filter those numbers to not start from 1 again. Only way i see it possible is creating an action orders table and saving all the info in the table then with the self incrementing order number column i can read it into my datatable but i need to asign the number to an order before im saving it to sql sort of like an overview of what you purchased then they have the option(button) to save the sale to sql
CHill60 6-Sep-18 8:23am    
If you are creating orders then surely you are storing those orders somewhere? If it is in a SQL database just keep an identity column on the orders table itself. You can create the order immediately and then either delete it or change a status if it is not proceeeded with. Or you only "assign" an order number if they confirm they are preceding ... you don't need an order number in order to preview the purchase
Member 13475897 6-Sep-18 8:19am    
or do i just select all the number out of my table and select the last record, throw it in a variable and start the list from that variable?
Member 13475897 6-Sep-18 8:21am    
If i do it with the variable way do i just create a new record in sql everytime they click the button where an order number is stored?

1 solution

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:
C#
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));

// Add columns to a new DataTable.
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(); //temporary remove data
dt.ReadXml(myxml);//load again


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?[^]
 
Share this answer
 
v2
Comments
Member 13475897 6-Sep-18 8:32am    
Thanks wil do
Member 13475897 6-Sep-18 8:38am    
My data table works in a spesific way, each time they click the button create order it takes the amount of water they ordered and works out the price then it shows all this info in my data table with the columns order number, amount of litres and price. the order number column is on auto increment but the problem im getting is that each time they cick the button the previous info in the data table is wiped and the new info is stored. It doesnt build up because its just a way for the cashier to view the order then the cashier can choose to save it to sql with a seperate button where my order number included is getting saved. i dont need that order number for sql purposes i need it to for the cashier to delete a record in the datagridview they dont want because they use the order number and type it in a text box provided then the record matching that order number is deleted on a seperate button click
Member 13475897 6-Sep-18 8:38am    
i'll quickly read up on that save xml solution
Maciej Los 6-Sep-18 8:42am    
Check my answer again and find out example code.
Member 13475897 6-Sep-18 8:43am    
So i dont want to store the whole datatable just the spesific order numbers to be able to read new order numbers next time. The rest of the datatable's info should not be saved and recalled to next time and show it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900