Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
Password Reminder
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #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

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:
UPDATE products SET productCode = 'P' & Right( '000' & CStr(pnum), 3 ) WHERE productCode IS NULL

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 07:33 AM.

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