Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In my project i want to set autogenerated code, like EB-1,EA-1. So how to set that any idea? I can set only in numeric that code is as bellow:

C#
public string autoGenerateemployeecode()
    {

        var countFirstServiceProvider = 101;
        string Type_ID = Convert.ToString(bindApplicantTypeID());
        dt.OpenCon();
        try
        {
            string queryCount = "select count(*) from master_applicant where MA_App_Code='" +Type_ID+ "'";
            decimal firstServiceProvider = Convert.ToDecimal(dt.ExecuteScalar(queryCount));
            if (firstServiceProvider > 0)
            {
                SqlDataReader dr = dt.ExecuteReader("select max(convert(numeric,ma_app_code))+1 as ma_app_code from master_applicant  where MA_Appl_type_id='" + Type_ID + "'");
                while (dr.Read())
                {
                    txtEmpCode.Text = dr[0].ToString();
                }
            }
            else
            {
                txtEmpCode.Text = countFirstServiceProvider.ToString();
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            dt.Dispose();
        }
        return (txtEmpCode.Text);
    }
Posted
Updated 23-Mar-15 19:21pm
v2
Comments
Er. Puneet Goel 24-Mar-15 1:16am    
What is the purpose for that? i will help once I understood your target!
Arkadeep De 24-Mar-15 1:24am    
whats the problem
Member 11221185 24-Mar-15 1:38am    
See i want to set autogenerated column on txtEmpCode.Text this text box.
i can able to set numeric no as per given code but i want to set with nvarchar or varchar no.

1 solution

You can use something like

C#
int codeLength = 3;
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            string result = new string(
                Enumerable.Repeat(chars, codeLength)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());

            //insert '-' special char at 2nd position, you can change the way you want
            result = result.Insert(2, "-");
 
Share this answer
 
Comments
Member 11221185 24-Mar-15 2:33am    
I want to set as EB-1 and EA-1 not a random no.
Er. Puneet Goel 25-Mar-15 0:52am    
Ok so your pattern will be like EA-1, EB-1, EC-1 or some other format you have?
Member 11221185 25-Mar-15 1:01am    
yes it is like only EA-1 and EB-1. So what can i do?
Er. Puneet Goel 25-Mar-15 4:50am    
basically this is not random string you want...so you have to code it on your own....! Jist give it try..its not hard. You have the logic. I will help out.

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