I'll assume that this question is not only about formatting a text, but also about id continuity in the context of multiple concurrent users. There are 2 scenarios:
You want to get the auto generated ID before the form is saved and show it to the user.
If this is the case, it's quite tricky, because of user concurrency. If 2 users will open a form, you will have to assign to each other the next auto incremented value (probably form the database or other persistent storage). If you do that, they will have consecutive ids. But if the first user cancels the form while the second user is filling it, this will break the continuity of the auto incremented part of the field (the second user already has an id which is skipping the one before, which was canceled). As the only workarounds for this scenario, you have to either break the ID continuity or replace it after save with the new value (which doesn't make too much sense).
You will show the auto generated ID to the user after the form is saved.
This is quite easy to do, and the best way to do it (in my opinion) is to send the user option (A/B/C/D
), eventually also the last 2 digits of the year to a database stored procedure, which will handle the creation of the auto generated id through string concatenation. All you have to do after that is return the auto generated ID from the database.
[UPDATE]
If you go for the second scenario, I'll post an example with the things you have to do. I'm assuming you are using MS SQL Server and stored procedures are also an option for you.
First, I will use in this example a simple form:
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="ddl">
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="C" Value="C"></asp:ListItem>
<asp:ListItem Text="D" Value="D"></asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" ID="btn"
OnClick="btn_Click" Text="Add form" />
<asp:Label runat="server" ID="label" />
</div>
</form>
Add the method which handles the button click:
protected void btn_Click(object sender, EventArgs e)
{
var year = DateTime.Now.Year.ToString();
var yearDigits = year.Substring(year.Length - 2);
var generatedId = SaveForm(yearDigits, ddl.SelectedValue);
label.Text = generatedId;
}
Add your connection string somewhere where it's accessible from the SaveForm
method:
const string connectionString = @"yourConnectionString";
Add the method which performs the insert and returns the auto generated id:
private string SaveForm(string yearDigits, string userOption)
{
string autoId;
using (var sqlConnection = new SqlConnection(connectionString))
{
try
{
sqlConnection.Open();
using (var sqlCommand = new SqlCommand("AddForm", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.Add("year", SqlDbType.Char, 2)
.Value = yearDigits;
sqlCommand.Parameters.Add("option", SqlDbType.Char, 1)
.Value = userOption;
var outputParameter = new SqlParameter();
outputParameter.ParameterName = "autoId";
outputParameter.Direction = ParameterDirection.Output;
outputParameter.SqlDbType = SqlDbType.Char;
outputParameter.Size = 14;
sqlCommand.Parameters.Add(outputParameter);
sqlCommand.ExecuteNonQuery();
autoId = outputParameter.Value.ToString();
}
}
catch (Exception)
{
// TODO: manage exception
throw;
}
}
return autoId;
}
On the SQL server side, we'll be using a simple table:
CREATE TABLE [dbo].[Forms](
[Id] [int] IDENTITY(1,1) NOT NULL,
[GeneratedId] [char](14) NOT NULL,
CONSTRAINT [PK_Forms] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
)
Now, add the stored procedure which will insert the dorm data and will return the autogenerated id:
CREATE PROCEDURE [dbo].[AddForm]
@year char(2),
@option char(1),
@autoId char(14) output
AS
BEGIN
DECLARE @rows int
SELECT
@rows = (SELECT COUNT(*) FROM [Forms]),
@autoId = 'FF-'+@year+'-'+Replace(Str(
CASE
WHEN @rows = 0 THEN 1
ELSE
IDENT_CURRENT('Forms')+1
END
, 6), ' ' , '0')+'-'+@option
INSERT Forms (GeneratedId) VALUES (@autoId)
END
That should be all.