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

Can you please send me the code for generating the Unique ID manually through code which is combination of prefixed string which is constant and postfixed by number which is increasing at each time of registration.

For Example,
I want to generate the Id through code. Whose output should be as follows,

org001
org002
org003 etc...

Thanks in advance.
Posted

The code for that is trivial: just increment a counter and properly format a string with the fixed prefix and the current value of the counter, e.g.
C#
string newID(string prefix)
{
  counter++;
  return prefix + counter.ToString("D3");
}


However counter needs to be persistent, that is you have to save it to a file if you need to maintain uniqueness on application restarts.
 
Share this answer
 
Comments
Prasad_Kulkarni 29-May-12 5:13am    
Good one 5!
CPallini 29-May-12 5:25am    
Thank you.
C#
SqlConnection cnn = new SqlConnection(your connection String);
    SqlCommand cmd;
    static string path;

    protected void Page_Load(object sender, EventArgs e)
    {

     
        cnn.Open();
       
        string str = "Select count(*) from  tablename";
        cmd = new SqlCommand(str, cnn);
        object necd;
        int newecd;
        cmd = new SqlCommand(str, cnn);
        necd = cmd.ExecuteScalar();
        newecd = Convert.ToInt16(necd) + 1;
        string necode;
        necode = "org00"  + newecd.ToString();
        txtccode.Text = necode.ToString();
        cnn.Close();
    }


may this code help you.....
 
Share this answer
 
v3
Comments
Prasad_Kulkarni 29-May-12 5:13am    
My 5!
try like this

C#
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
     da = new SqlDataAdapter("select id from sample order by id desc", con);
      ds = new DataSet ();
      da.Fill(ds);


      string orgname = "AAA";
      if (ds.Tables[0].Rows.Count > 0)
      {
          i = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
          i = i + 1;
      }
      else { i = 1; }
      string empname = orgname + Convert.ToString(i);

     if(con.State ==ConnectionState.Closed )
         con.Open ();
     cmd = new SqlCommand("insert into sample(id,name,address) values('" + empname + "','" + txtname.Text + "','" + txtaddr.Text + "')", con);
     int j = cmd.ExecuteNonQuery();
     con.Close();
 
Share this answer
 
You need to concatinate the two strings
1. Get the constant string eg. ABC
2. Retrive the total count value from the database (eg. count = 5)
If you are using the SQL server as your database.
You can also use file to store the data.
Now add two string
suppose string str = ABC
and int value = count + 1;
now,
C#
string id = str + value.ToString();

Remember, you can only concatinate strings.
 
Share this answer
 

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