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.

3
  • How do you plan to create it in VB.Net and prevent duplicate ID's? Commented Jan 30, 2013 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. Commented Jan 30, 2013 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? Commented Jan 30, 2013 at 16:42

2 Answers 2

4

The same function in VB.NET:

Function CustomerNumber(id As Integer) As String
    return "C" & id.ToString("0000")
End Function
Sign up to request clarification or add additional context in comments.

Comments

2

This should do the same:

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

Demo

6 Comments

this will be "C456456" if input id is 456456, I'm not sure what OP was asking for , but this isn't 4 digits
Thanks tim for your time..:)
I aswered a lot of time before and my answer is the same! T_T
@SysDragon: Yes, but your answer results in a compiler error. ideone.com/hLKiCY You've probably meant .ToString("0000").
@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)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.