Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 12th, 2009, 10:52 PM
Friend of Wrox
Points: 686, Level: 9
Points: 686, Level: 9 Points: 686, Level: 9 Points: 686, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Kolkata, West Bengal, India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Auto Increment Alphanumeric Code

Hi All !!

I need to generate an Alphanumeric Code for products for my Application where I have to add products to the database starting with 'P001' , 'P002' and so on. Everytime I add a new product the code will check for the last code entered, increment it and generate the next code. For example if the last Code in the database is 'P085' then it will create a new code as 'P086'

The code is a Text field in Access Database.

If someone has a code for this in C# please give. Any help would be appreciated.

Thanks in Advance
  #2 (permalink)  
Old October 13th, 2009, 01:02 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Since it's an Access DB, you can't do it all in the DB, unfortunately. With MySQL or SQL Server (or Oracle or...) you could.

But the answer is trivial:

Create *another* field, say call it PNUM, which is a simple AUTOINCREMENT (aka AUTONUMBER, aka COUNTER) field.

Create you PRODUCTCODE field as a CHAR(4) or VARCHAR(4) or whatever works for you, giving it a default value of NULL (which you don't have to specify...that's what it will be by default).

And every timy you insert a record, PNUM will increase. And *AFTER* each insert, you just then execute a simple SQL query:
Code:
UPDATE products SET productCode = 'P' & Right( '000' & CStr(pnum), 3 ) WHERE productCode IS NULL
Presto.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding auto increment of id Rajesh225 ASP.NET 1.0 and 1.1 Basics 1 June 27th, 2007 05:35 PM
Auto increment prad_a MySQL 3 April 7th, 2007 05:47 AM
How to auto increment ? Shawn Mohan SQL Server 2000 2 June 22nd, 2006 03:00 AM
How to auto increment? Shawn Mohan ASP.NET 2.0 Basics 6 June 20th, 2006 10:36 PM
auto increment? hosefo81 PHP Databases 1 February 2nd, 2004 03:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.