Click here to Skip to main content
15,886,815 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
how to auto genrate ID in textbox start from 1?
if we write the product name in another textbox then product id is 1.
and i add another product name then automaticall id asign is 2....
Posted
Comments
[no name] 9-Aug-12 20:51pm    
What have you tried? Where is the code that demonstrates any sort of a problem? What event in the second textbox are you using the addition?
Arsalaan Ahmed 9-Aug-12 21:01pm    
int counter = 1;
textBox1.Text = counter.ToString();
counter++;
Sergey Alexandrovich Kryukov 9-Aug-12 21:09pm    
Won't generally work because a counter is a local variable. By definition, the functionality should work repeatedly, when each method is returned.
Please see my answer, it works.
--SA
Volynsky Alex 11-Aug-12 3:55am    
Please read following:
http://stackoverflow.com/questions/2229019/dynamically-generate-textbox
http://stackoverflow.com/questions/3545305/creating-unique-id-for-textbox
http://stackoverflow.com/questions/3545305/creating-unique-id-for-textbox

In a simplest case, you could have a static field (uint would do fine) of some class which would initialized with 1 and be accessible from a property the way each request would increment the value:
C#
internal uint Id {
   get { return id++; }
}
private static id = 1;


Beware: this code is not thread safe. If you need to access Id from different threads, make sure you do it with lock:
http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

Now, working with static needs extra care and better be avoided. This field may not be static if the declared class has only one instance per run time, guaranteed. If does not have to be a singleton, and it better should not, if you use this Id only in one place, but the singleton pattern is the most typical universal approach to that. Please see:
http://en.wikipedia.org/wiki/Singleton_pattern[^].

See also:
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial[^].

I saw many bad singleton implementations. This is a good one, for C#:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA
 
Share this answer
 
v2
Hi,

Hope you are doing some work related with the database. If you are inserting entry in the database then you should not write code for ID in the C# instead you should add AutoIncrement column in the database.

If you write your code for generating ID value from C# then there are more issues may come. you will not understand those issue for now.

Check this article for : Autogenerated Column in SQL Server[^]

When you are adding some value in the name textbox generate one dummy value in the database and that will gives you one ID from it. If user press save button then update it otherwise discard that value. this way you can do that.

Although SA gives really good examples to do the same from C# but this can be other way to do your work.

Hope this information helps you,

Thanks
-Amit Gajjar.
 
Share this answer
 

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