sara
sara

Reputation: 305

Insert random number in database column using linq in asp.net

how to Update table column with random numbers using linq in asp.net. Please take a look at my code. same number is updated for all records in the list. what is going wrong in my code.

foreach(var item in list)
            {
                int rnd = 0;
                //item.RandomNumber = GetRandomNumber();
                Application app = new Application ();

                app.Id = item.Id;
                 rnd = GetRandomNumber();
                app.RandomNumber = rnd;
                listapp.Add(app);
            }

public int GetRandomNumber()
{
    int Random = 0;

    Random random = new Random();
    Random = random.Next(1, 99999);
    return Random;
}

It should update unique number for each list. but now column is updated with same number.

Upvotes: 0

Views: 205

Answers (1)

mdnghtblue
mdnghtblue

Reputation: 1117

Define the Random object outside of the foreach loop. When you create it on each iteration of the loop, it is likely being seeded with the same value each time, which gives you the same "random" number each time.

Upvotes: 1

Related Questions