Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all. I have to insert an Orders form where I have to insert fields like Order_Num, Order_Date, Product_ID, Order_Amount, etc... My problem is that the Order_Num (which is the record ID) is preformatted like this: <P1109-0001> where:
VB
"P" constant character (PurchaseOrder); "11" is the current year '2011'; "09" is the current month (Sept); "-" is constant character (separator); "0001" is the series number.
This series number increments whenever a new record is saved to the SQL table "Orders table".

In VB6, I am able to loop through the table to search for a duplicate ID and when there is no duplicate, the record with that ID is inserted.

Can you please guide me on how to do it in VB.net? I am not also familiar with stored_proc in SQL so I usually code using VB.

Thanks in advance.
Posted

You should have another table in the database that stores your series number. Then you can just retrieve this number whenever you want to insert a new purchase order, create your unique id, increment the series number and save all to database.

Hope this helps
 
Share this answer
 
store procedure takes the tablename, columnname as input parameter and retruns the next number
Employee table will return output as E11090001

SQL
create proc GetNextNumber
@tableName nvarchar(100),
@columnName nvarchar(100)
as
declare @strSql nvarchar(1000)
set @strSql = ' select ''' + upper(right(@tableName,1)) + ''' + 
		left(convert(nvarchar,GETDATE(),12),4) + 
		right(''0000'' + convert(nvarchar,max(right(' +@columnName + ',4))+ 1),4) 
	from ' + @tableName
exec sp_executesql  @strsql
 
Share this answer
 
v2
Hi Wayne, the series number resets to zero when the Year or the Month changes. For example, the Orders record <P1108-0326>
VB
(2011-Aug, series# 326)
will be expected to set to P1109-0001 once the date changes to September 01 (2011-Sep-01).
 
Share this answer
 
hi sachin10d, I will try this procedure. However, I have to study how to call SQL storedproc in dotNet. Thanks.
 
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