How to make an auto increment number based on a year
I have figured it out myself and thank you for the down votes
public class SerialNumber
{
private static readonly MyDbContext DbContext = new MyDbContext();
public static string SrNo()
{
var sn = DbContext.PrimaryForms.ToList().Last().FormDescription;
var array = sn.Split('/');
if (int.Parse(array[0]) == DateTime.Today.Year)
{
return array[0] + "/" + (int.Parse(array[1]) + 1);
}
return (int.Parse(array[0]) + 1) + "/1";
}
}
if code is not clear, ask me for explanation, thank you.
Auto Increment the number
in c#, you can do this
string s = "UK";
int counter = 0;
if (counter < 10000)
result = s + counter++.ToString().PadLeft(4, '0');
else
result = s + counter++.ToString();
Output: UK0000,UK0001,UK0002......
how to auto increment the number to increment by 1 after saving
int versionNumber = 1000;
public void versionIncrement()
{
versionNumber++;
lblOutput.Text = versionNumber.ToString();
lblOutput.Visible = true;
}
Best practices for auto increment a number EF Core
One way would be to do the following;
1) Use integers as keys, and set ClientNumber property to be nullable.
public class Bar
{
public int BarId { get; set; }
public ICollection<Client> Clients { get; set; }
}
public class Client
{
public int ClientId { get; set; }
public Bar Bar { get; set; }
public int BarId { get; set; }
public int? ClientNumber { get; set; }
}
2) Set ClientId to identity (automatic increment) and ClientNumber to database generated (otherwise EF Core will not re-read the value from database after insert)
entity.Property(e => e.ClientId).ValueGeneratedOnAdd();
entity.Property(e => e.ClientNumber).ValueGeneratedOnAddOrUpdate();
3) Add a trigger to modify the ClientNumber after insert
create trigger ClientNumber on Clients after insert
as
begin
set nocount on;
update c set ClientNumber = n.RowNum from Clients c
inner join (
select ClientId, ROW_NUMBER() OVER(PARTITION BY BarId ORDER BY ClientId ASC) as RowNum
from Clients
) n on n.ClientId = c.ClientId
where c.ClientId in (select ClientId from inserted)
end
Now all works automatically on insert:
var bar1 = new Bar() { Clients = new List<Client>() };
var bar2 = new Bar() { Clients = new List<Client>() };
bar1.Clients.Add(new Client());
bar1.Clients.Add(new Client());
bar2.Clients.Add(new Client());
bar2.Clients.Add(new Client());
context.Bars.AddRange(new[] { bar1, bar2 });
await context.SaveChangesAsync();
bar1.Clients.Add(new Client());
bar2.Clients.Add(new Client());
bar1.Clients.Add(new Client());
await context.SaveChangesAsync();
Will render
ClientId BarId ClientNumber
1 1 1
2 1 2
6 1 3
7 1 4
3 2 1
4 2 2
5 2 3
Downside is that you cannot use the unique index IX_CLIENT_NUMBER since ClientNumber will be NULL until the trigger executes.
How to auto-increment number and letter to generate a string sequence wise in c#
Looking at your code that you've provided, it seems that you're backing this with a number and just want to convert that to a more user-friendly text representation.
You could try something like this:
private static string ValueToId(int value)
{
var parts = new List<string>();
int numberPart = value % 10000;
parts.Add(numberPart.ToString("0000"));
value /= 10000;
for (int i = 0; i < 3 || value > 0; ++i)
{
parts.Add(((char)(65 + (value % 26))).ToString());
value /= 26;
}
return string.Join(string.Empty, parts.AsEnumerable().Reverse().ToArray());
}
It will take the first 4 characters and use them as is, and then for the remainder of the value if will convert it into characters A-Z.
So 9999 becomes AAA9999, 10000 becomes AAB0000, and 270000 becomes ABB0000.
If the number is big enough that it exceeds 3 characters, it will add more letters at the start.
Related Topics
Asp.Net Core 2 Web API Timeout Issue
How to Compare Key/Value Dictionary With == Operator on a Ireadonlycollection<String>
Getting Value of Selected Item in List Box as String
How to Ignore the Certificate Check When Ssl
Epplus: Find If the Entire Row Is Empty in Excel
Using C# Lambda to Split String and Search Value
Check If Database Exists Before Creating
Checking Whether a Number Contains Numbers 1 to N as Factors
How to Change Background Colour of Tab Control in Winforms
401 Unauthorized on Second Httpclient/Httpwebrequest Call
Moq - Verify Exception Was Thrown
Model Id Property Null in ASP.NET MVC C#
Asp.Net MVC - How to Call Void Controller Method Without Leaving the View
Select Multiple Fields Group by and Sum
C# File.Delete, File Being Used by Another Process
How to Hide a Property in Webapi
How to Add Json to Restsharp Post Request
Using Newtonsoft to Deserialize a Date Stamp That Might Consist Only of a Year