0

i just need to auto generate id like abc101,abc102,abc103,....abc10n and it should be store in DB table. and also it should be show in any textbox at runtime .

Please give a proper solution....

Special Thanks in advance :)

Regards.

Pradeep Kodley

pradeepKodley
  • 1
  • 1
  • 1
  • 1
  • 9
    Don't ask "send me teh codez" type of questions. Show what you have so far, and what is causing you problems. – Klaus Byskov Pedersen Apr 09 '10 at 10:31
  • Please, give a context to your question. Must the id be unique? What is this id based in? Any value to consider? What's the purpose of it? – jweyrich Apr 09 '10 at 10:32
  • Pradeep im using Asp.Net3.5 with c# and SqlServer2005 i just want to generate student unique id automatically. like abc101,abc102...through which i can identify each student by there unique id...same thing i have to do for hostelStd_id (hostel student id) – pradeepKodley Apr 09 '10 at 10:50
  • 1
    What are you using to access the database? LINQ? Entity? NHibernate? ADO? If you plan to use this id as a mean to find a student, better use an IDENTITY field as primary key in your MSSQL database. – jweyrich Apr 09 '10 at 11:00

6 Answers6

2

If every ID has the same prefix, then the prefix is a waste of space. Just use an autoincrementing integer and add the prefix in reports and stuff.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
2

Your DB probably has an auto-incrementing column for its database tables. Could you tell use more about your problem and what you want to do?

moogs
  • 8,122
  • 8
  • 44
  • 60
2

you can achieve this by creating a trigger in your db which would run before insertion of any record.

In your trigger, 'abc' + (MAX(idColumn) + 1) would fetch you the next value

You can also create a method in c# which would fetch the last id, increment it and insert it

Amsakanna
  • 12,254
  • 8
  • 46
  • 58
1

You can use GUID:

System.Guid  guid = System.Guid.NewGuid ();
String id = guid.ToString();
Hun1Ahpu
  • 3,315
  • 3
  • 28
  • 34
  • @Hun1Ahpu: This would generate unique ID, but not what op wants, which is a sequential unique ID. – KMån Apr 09 '10 at 10:37
  • 2
    @KMan: Nowhere he says that the ID must be sequential. And his comment to his own question suggests that he is not specially interested in the sequence, just the unicity. – Gorpik Apr 09 '10 at 11:03
  • @Gorpik: Read the first line `abc101,abc102,abc103,....abc10n`. – KMån Apr 09 '10 at 11:13
  • 1
    @KMan: I had read it. Even though IDs are consecutive in the example, OP does not mention it again. He is asked in the comments to clarify and he only says that he needs IDs to be unique. – Gorpik Apr 10 '10 at 17:17
1

Your database can generate the sequential IDs for you.

  1. Create an ID column in your database (I am guessing you are using MS SQL because you stated only C#).

  2. Set Identity column to true on the ID column

  3. Set Autoincrement to true on the ID column

The ID column value will be created automatically based on existing items in the database and you will get a sequence 1,2,3,.. for your student IDs. This column can also become the primary key for your Students table.

Learn about auto increment columns for different RDMBSes here.

Marek
  • 10,307
  • 8
  • 70
  • 106
0

Try this one:

con.Open();
            string sqlQuery = "SELECT TOP 1 kode_user from USERADM order by kode_user desc";
            SqlCommand cmd = new SqlCommand(sqlQuery, con);
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                string input = dr["kode_user"].ToString();
                string angka = input.Substring(input.Length - Math.Min(3, input.Length));
                int number = Convert.ToInt32(angka);
                number += 1;
                string str = number.ToString("D3");

                txtKodeUser.Text = "USR" + str;
            }
            con.Close();
Peter Badida
  • 11,310
  • 10
  • 44
  • 90