2

I would like to generate a sequence of numbers as "A0000"(ie.. an alphabet followed by 4numbers not randomly generated numbers).

I have gone through this article which is done with Sql Server similarly how can I achieve it in VB.Net.

Here is the code for SQL Server:

create function CustomerNumber (@id int) 
returns char(5) 
as 
begin 
return 'C' + right('0000' + convert(varchar(10), @id), 4) 
end

Any suggestions are welcome.

coder
  • 13,002
  • 31
  • 112
  • 214
  • How do you plan to create it in VB.Net and prevent duplicate ID's? – mellamokb Jan 30 '13 at 16:33
  • I want the user ID to be generated automatically on the form load event not accepting any duplicates by checking it with the previously entered values in database. – coder Jan 30 '13 at 16:35
  • Is the application used by only a single user at a time? What if user A loads the form, gets an ID, then user B loads the page as well before user A finishes submitting the form and gets the same ID? – mellamokb Jan 30 '13 at 16:42

2 Answers2

4

The same function in VB.NET:

Function CustomerNumber(id As Integer) As String
    return "C" & id.ToString("0000")
End Function
SysDragon
  • 9,692
  • 15
  • 60
  • 89
2

This should do the same:

"C" & id.ToString("d4")

Demo

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • this will be "C456456" if input id is 456456, I'm not sure what OP was asking for , but this isn't 4 digits – Scott Selby Jan 30 '13 at 16:50
  • Thanks tim for your time..:) – coder Jan 30 '13 at 16:59
  • I aswered a lot of time before and my answer is the same! T_T – SysDragon Jan 30 '13 at 17:00
  • 1
    @SysDragon: Yes, but your answer results in a compiler error. http://ideone.com/hLKiCY You've probably meant `.ToString("0000")`. – Tim Schmelter Jan 30 '13 at 17:01
  • @TimSchmelter Oh come on. Quotes left. Its hard enought for us to gain reputation points against pros like you. I answered sooner and is the same code. Not fair =/ – SysDragon Jan 30 '13 at 17:03
  • 1
    @SysDragon: 1. this was the first working solution 2. i'm not the one who accepts 3. `ToString(0000)` is not the same as `ToString("d4")` 4. upvoted your answer because it works too (it's not _fair_ to downvote competitive answers btw) – Tim Schmelter Jan 30 '13 at 17:08