0

This all will happen when the user hits the submit button

My requirement : ID Format should be FF-YY-00001-A (this is the field of the table)

1 FF is the shortcut Name

2.YY is the Current Date and I want to save this in table as year field ,so that it will help to track down the reports, condition: I have to take the last part of the year ie suppose 2013=> 13 2014=> 14

next field is autoincrement field ,for this it should be generated and incremented by one I m thinking ,this value can come from the autoincrement field from table like Serial no. like sr.no=1 we can take this field 00001 or 1 00001 (should be better if we can)

also we have to increment this value to 1 [* We have to get the previous value for the comparision and we can do with help of last record and field sr.no]

4.A/B/C/D is the option from the user

After this all

now our ID should be FF-13-1-A next: FF-13-2-B next entry: FF-13-3-C

Note: We have to consider the Dash (-) also ie our ID Format includes Dash ie (-)

After Submit Button :

a) ID Field will be updated example: FF-YY-00001 b) Autoincrement field Sr.no gets the value example: like 1 c)year field will be updated example: like 2013

Hope u will understand the requirement

user2139057
  • 1
  • 1
  • 7
  • _00001...00004 is autoincrement value_ + _then next time another user when open form it assing the newly generated ID_, so it comes from the database? – Alex Filipovici Mar 06 '13 at 09:54
  • what's the relation between "form" (what's "form" by the way?) and db row, column, table ? The scenario is really not clear (at first glance) – Raphaël Althaus Mar 06 '13 at 09:54
  • Do you need to show the auto generated ID before saving? – Alex Filipovici Mar 06 '13 at 10:01
  • yes brother u understand exactly what my requirement is,This Id will be created when the new entry is going to be submitted ,data will be populated in gridview.. – user2139057 Mar 06 '13 at 12:05

2 Answers2

1

Maybe this will help you:

int clientId = 3;
int otherId = 4

var id = String.Format("FF-{0}-{1:D5}-A", otherId, clientID);

Where you can set letters by replace FF as a {arg}. You can get the values from dropdownlist by calling dropdownlist.SelectedValue

+

Generating an array of letters in the alphabet

Community
  • 1
  • 1
Gregory W
  • 11
  • 4
0

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:

  1. 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).

  2. 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.

Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78