Click here to Skip to main content
15,917,627 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi, I am working in C#.net and taking ms-access as backend for windows application. I want to auto generate one number in a particular pattern(eg. AB/2014/2/2).
Here AB: will be some fixed string
2014: present year
2:month
5:auto generate no.(will increment after submission of form)

whenever some one will fill the form and will click on submit button then this no. has to be generated. last digit in this no. is sequence no. which will be incremented after checking previous no.
this no. will act as a primary key in database.

can somebody help me out with this? I am not getting how to code this.....
Posted

Try like this,
do some little homework..

C#
public string GenerateNext(string text)
   {
       string check = string.Format("{0}/{1}/{2}/",text,DateTime.Now.Year,DateTime.Now.Month);
       // query the DB to filter the primary key by passing the 'check' as Start with 'Like' Search
       // and return only the primary key values in a list of string or datatable
       List<string> lstValues = new List<string>();
      // lstValues =   GetDBValues(check); // you need to write logic for this..
       if (lstValues.Count == 0)
           return check + "0";
       else
       {
          int nextID =  lstValues.Select(k=>  k.Replace(check,"")).Select(k0=> Convert.ToInt32(k0)).Max() + 1;
           return check + nextID.ToString();
       }


   }
 
Share this answer
 
Comments
Diya Bh. 5-Feb-14 6:12am    
int nextID = lstValues.Select(k=> k.Replace(check,"")).Select(k0=> Convert.ToInt32(k0)).Max() + 1;

thanks. but can you please explain this statement?
Karthik_Mahalingam 5-Feb-14 6:44am    
this is LINQ..
add reference using system.Linq;
it will work.
Diya Bh. 5-Feb-14 6:48am    
thanku....
yeah now I got it...
Use an identify value in the database, and a computed column to generate the whole item.
Assuming you have an Identity field called Ident, a fixed text field call Prefix, and a DateTime field called InsertDate:
SQL
[ID]  AS (((((([Prefix]+'/')+CONVERT([varchar],datepart(year,[InsertDate]),0))+'/')+CONVERT([varchar],datepart(month,[InsertDate]),0))+'/')+CONVERT([varchar],[Ident],0))
 
Share this answer
 
Something like this? (i'm using a textbox just to try)

C#
string number = "";
string[] split = this.textBox1.Text.Split('/'); //I suppose here you'll get the last generated number from you database
number += split[0] + "/";

if (split[1] == DateTime.Now.Year.ToString("0000") && split[2] == DateTime.Now.Month.ToString("0"))
{
      //if actual year/month is the same as last generated number,generate next secuencial number
      number += split[1] + "/" + split[2] + "/" + (int.Parse(split[3]) + 1).ToString();
}
else
{      
      //month has changed,start with 0
      number += DateTime.Now.Year.ToString("0000") + "/" + DateTime.Now.Month.ToString("0") + "/0";
}
this.textBox1.Text = number;
 
Share this answer
 
Comments
Diya Bh. 5-Feb-14 6:00am    
this one is not working...
Pikoh 5-Feb-14 6:03am    
It's working..but i forgot to initialize the textbox with "AB/2014/2/1" for example,or even "AB/0000/0/0". If it's empty it doesn't work
Diya Bh. 5-Feb-14 6:18am    
yeah now its working.
but you had taken textbox for doing this.
I don't have 2 use textbox. This no. has to be generated only in code and not to be shown to the user.
Is there any other way?
Pikoh 5-Feb-14 6:40am    
well..just use a string variable (string pattern="AB/0000/0/0") and replace all occurences of this.textbox1.text with pattern. Textbox was just to try it,but the logic is valid.
Diya Bh. 5-Feb-14 6:51am    
thanks....:)
You can use Random class. That is a predefined class, make its object and use it.
 
Share this answer
 
you may refer this function...
private string GenerateRandomNumber()
{
Random rnd = new Random();
List<char> availableNumbers = new List<char>{'0','1','2','3','4','5','6','7','8','9'};
Dictionary<char,> counts = new Dictionary<char,int>();
for (int i = 0; i < 10; i++)
{
counts.Add(availableNumbers[i],0);
}
char[] generatedCharacters = new char[10];
for (int i = 0; i < generatedCharacters.Length; i++)
{
char digit = availableNumbers[rnd.Next(availableNumbers.Count)];
generatedCharacters[i] = digit;
counts[digit]++;
if (counts[digit] == 3)
availableNumbers.Remove(digit);
}
return new string(generatedCharacters);
}
 
Share this answer
 
Comments
Diya Bh. 5-Feb-14 6:06am    
thanks.
Can you please explain me "for" block?

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