Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am stuck in getting the next series number. Example :-

1) I started the series from 1 and on getting the next series number 2, i saved the records like this till 100.

2) Then I skipped the numbers till 149 and started the series from 150 to 200.

3) After that when i again change the series from 101 till 149, the next series number after 149 should come 201. How to solve this?
Posted
Comments
Maciej Los 16-Mar-14 7:02am    
What have you done? Where are you stuck? Sample code might be helpful.
agent_kruger 17-Mar-14 3:18am    
sir, for now i am using a recursive loop to get the next serial number which takes time if the records increase.
BillWoodruff 16-Mar-14 8:33am    
Show some code. Your current problem description could mean many different things.

Exactly how you do this will depend on your code - and we have no idea how that works.
But - assuming that you have a collection of values, it's not complex:
C#
List<int> existing = new List<int>();
for (int i = 1; i <= 100; i++) existing.Add(i);
for (int i = 150; i <= 200; i++) existing.Add(i);
int minUnused = Enumerable.Range(existing.Min(), existing.Max() + 1).Except(existing).Min();
 
Share this answer
 
Comments
agent_kruger 17-Mar-14 3:17am    
sir, this is what i need but my record exists in "datatable".
OriginalGriff 17-Mar-14 12:15pm    
So replace "existing" with the DataRows and build a List to pick the column with the ID.
agent_kruger 18-Mar-14 4:00am    
sir, i am getting all records from database that would make it heavy, can you give me another way to handle it on the database side.
Not sure what you want, but...

Have a look at example:
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> Serials= new List<int>();
            int tmpv =0;
            for (int i = 96; i <= 202; i += 2)
            {
                tmpv = GetNextSN(i);
                if(tmpv>0 && !Serials.Contains(i))
                {
                    Serials.Add(tmpv);
                }
                Console.WriteLine("Current value: {0}; SN: {1}",i,tmpv);
            }
            Console.ReadKey();
        }

        static int GetNextSN(int currSN)
        {
            int rv = currSN;
            if (rv <= 100)
                return rv;
            else
            {
                while (rv > 100)
                {
                    rv -= 100;
                }
                if (rv>=1 && rv<=49)
                    rv = currSN;
                else
                    rv = 0 ;
                return rv;
            }
        }
    }
}


Change the code to your needs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900