You must Sign In to post a response.
  • Category: ASP.NET

    Example for Generate unique id to user

    Hello Developers,
    I am developing a project now i want to generate a unique id for registered user.I have done but i don't got exactly output.Could all of you please give me a example for how to i am done it.
    my form is like as follows,
    name:xxx
    company name:xxxx
    Emailid:xxx
    mobile:xxx
    useruniqueid: should be insert

    after the user give the details the useruniqueid has been stored on the database like ,
    useruniqueid: TR0001

    but it is stored like TR0000 only how i get exactly output Friends.

    Thanks with
    Paul.S
  • #762205

    Hi Paul.

    string prefix="TR"
    string s = "0001";
    int num = Convert.ToInt32(s);
    num += 1;
    string str = num.ToString("D4");//now str value is 0002, 0003,0004,0005.... till your for loop condition gets false.

    string uniqueid= prefix+str;

    // uniqueid is TR0001,TR0002,TR0003.. so on

    Regards

    Sridhar Thota.


    Sridhar Thota.
    Editor: DNS Forum.

  • #762209
    Thanks Mr.Sridhar thota ,, Thanks a lot if i got any error i will update you. Thanking you.

    Paul.S

  • #762210
    Hi Paul.

    You are welcome. If you dont get out put as expected let me know.

    Regards

    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762211

    Hello Paul Raj,

    Refer the below code. It generates unique customer id from SQL table :

    int custid = 0;

    con.Open();
    cmd = new MySqlCommand("Select COUNT(ID) FROM CustMaster", con);
    dr = cmd.ExecuteReader();
    if (dr.HasRows) {
    dr.Read();
    custid = Convert.ToInt32(dr(0).ToString()) + 1;
    TextBoxCode.Text = "CUST00" + custid.ToString();
    if ((custid > 9)) {
    TextBoxCode.Text = "CUST0" + custid.ToString();
    }
    dr.Close();
    } else {
    TextBoxCode.Text = "CUST001";
    }
    con.Close();


    Refer the below code for generating unique id normally without SQL table :

    string uniqueid;

    private void ButtonGenerateUniqueID_Click(object sender, EventArgs e)
    {
    string prefix = "TR";
    if (listBox1.Items.Count == 0)
    {
    uniqueid = prefix + "0001";
    }
    else
    {
    int uid = listBox1.Items.Count + 1;
    uniqueid = prefix + "000" + uid.ToString();
    if (uid > 9)
    {
    uniqueid = prefix + "00" + uid.ToString();
    }
    else
    {
    uniqueid = prefix + "000" + uid.ToString();
    }
    }
    listBox1.Items.Add(uniqueid);
    }

    See the attached image. It's an output of above mentioned code.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762220
    Hi

    You can try this code


    DataTable dt = new DataTable();
    SqlDataAdapter sqladp = new SqlDataAdapter("Select Max(id) from TblEmp ", con);
    sqladp.Fill(dt);
    string genno = "";
    if (Convert.ToInt32(dt.DefaultView[0][0]) == 0)
    {
    genno = "TR000" + 1;
    }
    else
    {
    genno = "TR000" + Convert.ToInt32(dt.DefaultView[0][0])+1;
    }
    TxtGenNo.Text = genno;


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments