• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 81
  • Last Modified:

ASP.NET : Generate Unique Number of length 4 alphanumeric length where first character always start with alphabet

Hi EE,

I need help in generating the Unique Number of 4 character length.

Criteria is as follows :
  1. String will always start with alphabet
  2. Another 3 characters will be a combination of alphabets (A-Z) and digits (0-9)
  3. Once the number generated it could not be repeat

Example
A001, A999, ZD01, DA9P, SXS8 etc...

I need to write a logic for the same in my ASP.NET (VB.NET) based web application.

Please provide a direction.

Regards,
D Patel
0
D Patel
Asked:
D Patel
  • 3
  • 2
1 Solution
 
Dorababu MSenior Software EngineerCommented:
Once the number generated it could not be repeat are you storing the generated number some where?
0
 
D PatelD Patel, Software EngineerAuthor Commented:
Yes in MySQL table
0
 
Ryan ChongCommented:
thinking to have a stored procedure for data insertion and then build the logic for your field accordingly there?

then in your VB.NET program, you can call that stored procedure for data insertion.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
D PatelD Patel, Software EngineerAuthor Commented:
Can you please brief about it more Ryan Chong?
0
 
Ryan ChongCommented:
well... not really have an example on hand, what i mean would be do "everything" at your database level, but if you saying to generate a unique key, you could simply use a Function instead:

DELIMITER $$
USE `yourDB`$$
CREATE FUNCTION `getRandomKey`(keylength INT) RETURNS varchar(255) CHARSET utf8
BEGIN

DECLARE newKey VARCHAR(255);
DECLARE isUnique BIT;

SET isUnique = 0;

 loop_label:  LOOP
 
	 SET newKey = CONCAT(SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ', RAND()*25+1, 1) , UPPER(LEFT(UUID(), keylength -1)));
	 SELECT COUNT(*) INTO isUnique FROM yourExisting_Table WHERE uniqueKey = newKey;

	 IF  isUnique = 0 THEN 
		LEAVE  loop_label;
	 END  IF;
            
 END LOOP;
   
RETURN newKey;
    
END$$

DELIMITER ;

Open in new window


then you can use this generated key for data insertion.
0
 
D PatelD Patel, Software EngineerAuthor Commented:
Thanks for your support.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now