Generate URL with random number using C# in ASP.Net

RichardSa
 
on Aug 17, 2021 04:58 AM
472 Views

I have a big school project at hand, it is about creating an online examination platform where and admin will create an examination with Examination Name, School Name, Exam Start Date and End Date (All these data will be saved into database) on button click, and a URL will be generated instantly for that particular Examination where the Admin will send to Students via email. This URL will also be saved into database alongside the Examination Details when the Admin saved them. The URL will be that of the webpage where the Examination Questions will be available for students when the students login. I have an instance where a short URL is generated on page load event but it only shows this:

CODE FOR GENERATING URL

    private void GetURL()
    {
        Random rand = new Random();
        for (int i = 0; i < 8; i++)
        {
            int random = rand.Next(0, 3);
            if (random == 1)
            {
                random = rand.Next(0, numbers.Count);
                URL += numbers[random].ToString();
            }
            else
            {
                random = rand.Next(0, characters.Count);
                URL += characters[random].ToString();
            }
        }
    }

I tried to add the URL of the questions page here but it did not work; like this:

"questions.aspx?id=" += numbers[random].ToString();
"questions.aspx?id=" += characters[random].ToString();

I changed it to the code below and it displayed the random characters and numbers as shown above

    private static List<int> numbers = new List<int>()
        { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
        private static List<char> characters = new List<char>()
        { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '_', '&'};

    protected void Page_Load(object sender, EventArgs e)
    {
        GetURL();
    }
     private void GetURL()
    {
        Random rand = new Random();
        for (int i = 0; i < 8; i++)
        {
            int random = rand.Next(0, 3);
            if (random == 1)
            {
                random = rand.Next(0, numbers.Count);
                Label1.Text += numbers[random].ToString();
            }
            else
            {
                random = rand.Next(0, characters.Count);
                Label1.Text += characters[random].ToString();
            }
        }
    }

How can I make it to have the address of the web page where the questions will be shown based on the Examination Name, Start Date and End Date, and also have unique generated URL? Please I need great help with this.

Thank you

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 17, 2021 05:03 AM

Hi RichardSa,

Refer below code.

Code

private static List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
private static List<char> characters = new List<char>()
    { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '_', '&'};

protected void Page_Load(object sender, EventArgs e)
{
    GetURL();
}
private void GetURL()
{
    Random rand = new Random();
    for (int i = 0; i < 8; i++)
    {
        int random = rand.Next(0, 3);
        if (random == 1)
        {
            random = rand.Next(0, numbers.Count);
            Label1.Text += numbers[random].ToString();
        }
        else
        {
            random = rand.Next(0, characters.Count);
            Label1.Text += characters[random].ToString();
        }
    }

    Label1.Text = "questions.aspx?id=" + Label1.Text;
}