Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello...

i work on one asp.net project in that i need one auto generate transaction id with prefix.it should be like vehicle identification number. which should be common id.
for that what should i do ?
Posted
Updated 24-Jul-13 19:05pm
v2
Comments
Jignesh Khant 25-Jul-13 1:20am    
While making the table of your vehicle table in database(SQL), create a column id and make it autogenerate. This will serve your purpose.
[no name] 25-Jul-13 1:22am    
no it not work i want all transaction id length with 10 or 12 char.
V.Lorz 17-Sep-13 7:18am    
Most databases support using 32 bit unsigned integers for the row id column value, that gives you around 4294967296 values, which is in the the range of 10-to-12 characters wide when represented in text format. Zero padding-left will complete your text length.
Jignesh Khant 25-Jul-13 1:30am    
Then you can assign a random number to each vehicle as its vehicle id when you insert it for first time.
[no name] 25-Jul-13 1:34am    
yes that want you are right but how can i do it ? and also it number must be uniq

Try this code:

C#
System.Random rand = new System.Random((int)System.DateTime.Now.Ticks);
        int random = rand.Next(1, 100000000);


Here variable 'random' will be your unique random number.
 
Share this answer
 
v2
Comments
[no name] 25-Jul-13 1:57am    
it's good but how can you sure that the number is unique ?
Jignesh Khant 25-Jul-13 2:01am    
int random = rand.Next(1, 100000000);

see this line, it means it will generate nos between the range 1-100000000 without repetition. You can always change this range according to your needs.
[no name] 25-Jul-13 2:06am    
yes but one time it generate '23450' number when i test it how can it possible ?
Jignesh Khant 25-Jul-13 2:09am    
I am not getting you.
V.Lorz 17-Sep-13 7:04am    
Random number generators are what their name says, Random, and that means it has inherent unpredictability. Random generators tend to follow some kind of 'distribution', it can be gaussian, normal, etc.

You may absolutely be sure that numbers in a random series will appear more than once with higher or lower probability denpending on the distribution function of the generator whether it be hardware (true generator) or software (pseudo generator).
C#
public string generateID()
{
    return Guid.NewGuid().ToString("N");
}


http://msdn.microsoft.com/zh-cn/library/system.guid.newguid.aspx[^]

<pre>Guid: Represents a globally unique identifier (GUID).</pre>
 
Share this answer
 
Comments
V.Lorz 17-Sep-13 7:11am    
Note that he wants to keep the id in the 10 to 12 chars range.
concatenate vehicle identification No with Max Transaction ID Like the following.
in the code VID2345 is vehicle ID
C#
SqlConnection con=new SqlConnection("Data Source=(local); Initial Catalog=salepur; Integrated Security=SSPI");
SqlCommand com=new SqlCommand(@"SELECT isnull(Max(cast(substring(tranID,charindex('-',tranID)+1,len(tranID)) as int)),0)+1
  FROM  [tbltran]",con);
con.Open();
int tranID=int.Parse(com.ExecuteScalar().ToString());
con.Close();
string TransactionID="VID2345"+"-"+tranID;
 
Share this answer
 
Comments
Member 10239720 17-Sep-13 3:14am    
;excepted error is coming at Textbox EmpID.Text and EmpId.Enabled.. what do you mean?
please clear it give me solution
Zafar A khan 28-Sep-13 2:56am    
Post your code please
you can use below trick
C#
DateTime.Now.ToString("ddmmyyhhmmss");


but i will suggest you to create one unique identity column in your table and concate that id with any prefix and use it
 
Share this answer
 
v2
Comments
V.Lorz 17-Sep-13 7:08am    
This is not appropiate for generating unique ids. If you have a fast computer you'll be able to generate the same id thousands or even millions of times during the time lapse of one second.

Furthermore, when Windows updates the internal RTC (real time clock) for synchronizing with time services you may run again in the same problem, your local RTC could have been running faster than the reference clock used by the time server.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System.Configuration;

public partial class Reg : System.Web.UI.Page
{
    string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    string str;
    SqlCommand com;
    int count;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            autogenerated();
        }
    }
    void autogenerated()
     { 
        SqlConnection con= new SqlConnection(strConn);
        str= "select count (*) from Reg";
        com = new SqlCommand(str,con);
        con.Open();
        count = Convert.ToInt16(com.ExecuteScalar())+1;
        TextBox EmpId.Text = count.ToString();
        TextBox EmpId.Enabled= false;
        con.Close();
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
      
        SqlConnection con = new SqlConnection(@"Data Source=VIJAY-PC\SQLEXPRESS;Initial catalog=Reg;Integrated Security=True");
        con.Open();
        string q = "Insert into Reg (EMPID,[EMP Name],[Account Holder],[Account Number],[Account IFSC Number],[Emp Email],[Emp Password],[Confrim Password],[Emp Photo])Values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "')";
        SqlCommand com = new SqlCommand(q, con);
        com.ExecuteNonQuery();
        con.Close();
        autogenerated();
        }   
    }
 
Share this answer
 
v2
Comments
V.Lorz 17-Sep-13 7:26am    
Note that using the query "select count (*) from Reg" will return the 'count', not the 'max'. It will fail if you remove any row from the table.

Locating the maximun value has also one 'little' problem, it is not thread-safe:
1) Thread-1 adds one row; <-this autoincrements the counter from (Val) to (Val+1).
2) Thread-1 gets preempted by Thread-2;
3) Thread-2 adds one row; <-this autoincrements the counter from (Val+1) to (Val+1+1).
4) Thread-2 reads the max id; <-it will receive (Val+2);
5) Thread-2 is preempted and Thread-1 enters again RUN mode;
6) Thread-1 reads the max id; <-it will receive (Val+2) <<<< ERROR!!!!!;

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