Click here to Skip to main content
15,888,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a form that loads two user controls. Basically a user types in information in 1st user control and then transfer him/her to the 2nd user control where they can click Apply which then saves information to a text file.

However I have a problem with an idea of how to create incremented product ID that will not repeat itself. What I mean by that is whenever in the 2nd form everything is saved to the text file a user can then exit the application or start all over again. But when starting all over again it will obviously take them to 1st user control to enter valid data but of course it will give to all the data types its default values.

So in other words, the product Id will start with its default value of 0.

I would like to make sure that the product id will always stay the same when user starts all over again to that it can be incremented to a new unique value.

NOTE: The start all over again button is located on the form not in the user control.

I anyone have an idea how could I achieve this?

Thanks in advance.

What I have tried:

For example,
Product ID = 1

user click to start all over again and the product ID should stay the same (1) so it then can be incremented to 2.

This is my Product Class:

C#
public Product()
{ }

public string ProductId //The product ID I want to be incremented.
{
    get { return productId ; }
    set { productId = value; }
}
Posted
Updated 10-Dec-17 21:46pm
Comments
FranzBe 9-Dec-17 15:43pm    
You need to store your ProductId outside your application and have it loaded on startup. I case you are connected to a database you can use a sequence for that. Alternatives might be: some key in the registry or a program-setting.

1 solution

If you want to keep your data after the program finishes you need some kind of data store (database or document).
But if you don't care for keeping data you can achieve what you ask with DataTable class.

C#
var myTable = new DataTable("Product");

var productidColumn = new DataColumn("ProductId", typeof(int));
productidColumn.AutoIncrement = true;
// primary key or just unique? You decide.

myTable.Columns.Add(productidColumn);


Then you can manipulate with data as you please.
Also you can go a step further and create typed dataset or code first EF repository.
There are many ready solutions for your requirement, last thing I would do is to make own custom auto-increment mechanism.
It is possible and simple, but it is also as reinventing the wheel (and prone to bugs in case of beginners!).
 
Share this answer
 
Comments
fellanmorgh 12-Dec-17 3:38am    
Interesting solution, I will check it out.

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