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

D Patel
D Patel used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dorababu MSenior Software Engineer
Distinguished Expert 2018

Commented:
Once the number generated it could not be repeat are you storing the generated number some where?
D PatelD Patel, Software Engineer

Author

Commented:
Yes in MySQL table
Ryan ChongSoftware Team Lead

Commented:
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.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

D PatelD Patel, Software Engineer

Author

Commented:
Can you please brief about it more Ryan Chong?
Software Team Lead
Commented:
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.
D PatelD Patel, Software Engineer

Author

Commented:
Thanks for your support.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial