help about avoiding duplication...

Last post 07-06-2009 4:56 AM by tapanrami@hotmail.com. 5 replies.

Sort Posts:

  • help about avoiding duplication...

    07-03-2009, 3:32 PM
    • Member
      13 point Member
    • mohit1122
    • Member since 07-03-2009, 3:38 PM
    • delhi,india
    • Posts 5

    hi..

    i want a value of 4 figures which is randomly generated by

    Random RandomClass = new Random();

    int RandomNumber = RandomClass.Next();

    to be inserted into a table but avoiding duplication...

    if the random value generated is already present in the table then i want it to calculate another value and so on until there is no duplication..

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

  • Re: help about avoiding duplication...

    07-03-2009, 6:05 PM
    • All-Star
      46,226 point All-Star
    • jimmy q
    • Member since 11-02-2006, 9:01 AM
    • Australia
    • Posts 3,184
    • Moderator
      TrustedFriends-MVPs

    mohit1122:

    i want a value of 4 figures which is randomly generated by

    Random RandomClass = new Random();

    int RandomNumber = RandomClass.Next();

    to be inserted into a table but avoiding duplication...

    if the random value generated is already present in the table then i want it to calculate another value and so on until there is no duplication..

    First of all, if you are going to limit it to 4 digits which means 0 - 9999 that is only 10,000 individual unique values so at some point you will clash.

    If this is acceptable then the second question, is does the number have to be random or can it be sequential, that is you will get 1 2 3 4 5 ... and so on.

    If it can be a sequential number then you can rely on the database to do this. Set up an integer column and enable the SEED with an increment on 1.

    What this does is on each insertion the database will assign a incremented value of 1 from the previous value.


    If it must not be sequential then you can continue using the Random class but you will need to check the database to see the integer is not used yet.


  • Re: help about avoiding duplication...

    07-04-2009, 8:53 AM
    • Member
      13 point Member
    • mohit1122
    • Member since 07-03-2009, 3:38 PM
    • delhi,india
    • Posts 5

    it must not be sequential....and i know to achieve this i have to serch the database but the problem is that if the random number generated is already in the database, how can i make it to generate another number until the right one is generated  ....i think a loop structure could be used but i dont know how?

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

  • Re: help about avoiding duplication...

    07-04-2009, 2:39 PM
    Answer
    • Member
      40 point Member
    • abdelnaby
    • Member since 07-02-2009, 1:07 PM
    • Posts 8

    Random RandomClass = new Random();

    while(true)

    {

    int RandomNumber = RandomClass.Next();

    if (!IsExistInDatabase(RandomNumber) )

    {

    InsertIntoDataBase(RandomNumber);

    break;
    }

     

    }

    Mohamed Abdel Naby
    Senior asp.net developer
    Egypt
  • Re: help about avoiding duplication...

    07-05-2009, 6:41 AM
    • Member
      13 point Member
    • mohit1122
    • Member since 07-03-2009, 3:38 PM
    • delhi,india
    • Posts 5

    thanx....that really helped..

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

  • Re: help about avoiding duplication...

    07-06-2009, 4:56 AM

     Fetch the data from the database table in the form of Data Reader and then compair each value with newly genearted value. if both match then generate new value

     

     

    Please mark as answer if you find this useful.

    Thanking You
    Tapan
Page 1 of 1 (6 items)