i just need to auto generate id like abc101,abc102,abc103,....abc10n and it should be store in DB table. and 开发者_JAVA百科also it should be show in any textbox at runtime .
Please give a proper solution....
Special Thanks in advance :)
Regards.
Pradeep Kodley
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.
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?
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
You can use GUID:
System.Guid guid = System.Guid.NewGuid ();
String id = guid.ToString();
Your database can generate the sequential IDs for you.
Create an ID column in your database (I am guessing you are using MS SQL because you stated only C#).
Set
Identity column
to true on the ID columnSet
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.
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();
精彩评论