Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8581803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:08:23+00:00 2026-06-11T21:08:23+00:00

Just some background about my application. I am developing an ASP.Net MVC 3 Web

  • 0

Just some background about my application. I am developing an ASP.Net MVC 3 Web App which uses Entity Framework 4.1 for data persistence. My application is tiered in that it has a UI layer, Service layer, Repository layer etc. I also use Unity for my Inversion of Control Container.

When a user registers on my application, I create two random codes (Email and Mobile verification codes) using the StringBuilder. I then assign these two random codes to their appropriate properties in a User Object, see below.

User validateUser = new User();

validateUser.firstName = model.firstName.Trim();
validateUser.lastName = model.lastName.Trim();
validateUser.email = model.Email.Trim();

//Create Email and Mobile Verification Codes
string randomEmailCode = "";
randomEmailCode = _notifyService.GenerateEmailCode();
validateUser.emailVerificationCode = randomEmailCode;

string randomMobileCode = "";
randomMobileCode = _notifyService.GenerateMobileCode();
validateUser.mobileVerificationCode = randomMobileCode;

NotifyService

public string GenerateEmailCode()
{
    StringBuilder builder = new StringBuilder();

    builder.Append(RandomString(4, true));
    builder.Append(RandomNumber(1000, 9999));
    builder.Append(RandomString(2, false));
    return builder.ToString();
}

public string GenerateMobileCode()
{
    StringBuilder builder = new StringBuilder();

    builder.Append(RandomString(3, true));
    builder.Append(RandomNumber(1000, 9999));
    builder.Append(RandomString(2, false));
    return builder.ToString();
}

private int RandomNumber(int min, int max)
{
    Random random = new Random();
    return random.Next(min, max);
}

private string RandomString(int size, bool lowerCase)
{
    StringBuilder builder = new StringBuilder();
    Random random = new Random();
    char ch;
    for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (lowerCase)
            return builder.ToString().ToLower();
        return builder.ToString();
    }

I then add the user to my DBcontext and call the SaveChanges() method to save the new User into my database.

_accountService.AddUser(validateUser);
_accountService.Save();

Now, when I go into my database and find the new User which has been added, the emailVerificationCode and the mobileVerificationCode are both the same. However, if I put a break point on my code at the following line

randomEmailCode = _notifyService.GenerateEmailCode();

And trace through until the Save, and then go check the database for the new User, both emailVerificationCode and the mobileVerificationCode codes are different, as expected.

I cannot understand why when I run the application it inserts the same code for both properties.

Can anyone please help with this?

Thanks.

UPDATE

I did as Jane suggested, ie, put in hard coded values for each property like so

validateUser.emailVerificationCode = "emailCode";
validateUser.mobileVerificationCode = "mobileCode";

Ran the application again, and this time the two hardcoded values were inserted as expected. Does this mean that that my two methods GenerateEmailCode() and GenerateMobileCode() are not working correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:08:24+00:00Added an answer on June 11, 2026 at 9:08 pm

    there is probably something wrong with the way you are generating those random codes.
    As others have pointed out, the problem is most likely that you are creating instances of the random class too close in time.

    try to declare a static random generator:

    static Random r = new Random();
    

    then use the static random to generate the code: (in what way you generate the code is up to you obviously but something like this should give you an idea):

     public static string generateCode()
            {          
                string chrs = "abcdefghijklmnopqrstuvwxyz";
                char[] arr = chrs.ToCharArray();
                string code = "";
                for (int i = 0; i < 5; i++)
                {
                    code += arr[r.Next(arr.Count())];
                }
                return code;
            }
    

    when i run this:

     var firstCode  = generateCode();
     var secondCode = generateCode();
    

    i get 2 different values.

    here’s another helpful link

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background Information Our application reads/writes from 3 components: ASP.NET MVC 3 customer front end
Just some background, sorry so long winded. I'm using the System.Data.SQLite ADO.net adapter to
A have ASP.NET 2.0 web application that should allow sending emails. I have a
A little background I'm working on an .net application that's uses plugins heavily, the
I come from a PHP background and I'm just getting my teeth into some
In some of my application designs or for just some UIViews, following a navigationController's
Right now, I've just some code which fetches the picture from the URL directly.
We use Wix to build an installer for our .NET application and are just
I am new to C++ from a C#.NET and Visual Studio background (and some
Background I am automating some Office application (Word and PowerPoint) via command-line tool. One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.