How to generate 5 digit random numbers and save in Database using C# and VB.Net in ASP.Net

Last Reply on Jan 24, 2017 09:20 AM By dharmendr

Posted on Jan 24, 2017 08:35 AM

I need to develop a web application with a textbox and button that will generate 5 digit random numbers based on the value given in textbox.Suppose if i give 7 in text box for the first time and click generate button i need to generate 7 five digit random numbers i.e 10001,20000,25431,98765,45321,54321,76578 like this 7 five digit random numbers and these generated random numbers should be saved in database.Again if i give another number in text box let it be 4 again i need to generate 4 five digit random numbers but this time the random numbers that are generated should not display the already saved random numbers.How can i do this can anyone help me out as i am new to this.

You are viewing reply posted by: dharmendr on Jan 24, 2017 09:20 AM.
Posted on Jan 24, 2017 09:20 AM
dharmendr says:
for (int i = 0; i < Convert.ToInt32(txtValue.Text.Trim()); i++)
        {
            int n = random.Next(0, 100000);
            number += n.ToString("D5") + "<br/>";
        }

You need to change the code like below. For insert code into database you have to check the data already present or not. If not present then insert into database.

for (int i = 0; i < Convert.ToInt32(txtValue.Text.Trim()); i++)
        {
            int n = random.Next(0, 100000);
            number = n.ToString("D5") ;
            // Code for insert the no to database.
        }