Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
How to generate auto id and show in textbox from button click event in .net c#
Posted
Comments
kingsa 3-Aug-12 4:52am    
tellin clear manner id from database value

hi..
this may help you.
http://www.dotnetperls.com/random[^]
 
Share this answer
 
v2
Hi,
You can use Random Class[^] to generate Random ID.
Try this:
C#
private string RandomString(int size, bool lowerCase)
{
    StringBuilder builder = new StringBuilder();
    Random random = new Random();
    char ch ;
    for(int i=0; i<size;>    {
        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
        builder.Append(ch);
    }
    if(lowerCase)
    return builder.ToString().ToLower();
    return builder.ToString();
}

This function will return the Random ID.


--Amit
 
Share this answer
 
declare a global variable and increment it on each click
C#
int id = 1;

//on button click
id++;
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace NewFurniture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Furniture.mdb");
        OleDbCommand cmd;
        OleDbDataAdapter da;
        OleDbDataReader dr;

        private void Button_Click(object sender, EventArgs e)
        {

try
            {
                textBox1.Enabled = false;

                int ctr, len;
                string codeval, code;
                DataRow drr;

                con.Open();
                da = new OleDbDataAdapter("select Customer_id from Customer",con);
                DataSet ds1 = new DataSet();
                da.Fill(ds1);

                DataTable dt;
                dt = ds1.Tables[0];
                len = dt.Rows.Count - 1;
                drr = dt.Rows[len];
                code = drr["Customer_id"].ToString();
                codeval = code.Substring(1, 3);

                ctr = Convert.ToInt32(codeval);

                if ((ctr >= 0) && (ctr < 9))
                {
                    ctr = ctr + 1;
                    textBox1.Text = "C00" + ctr;
                }
                else if ((ctr >= 9) && (ctr < 100))
                {
                    ctr = ctr + 1;
                    textBox1.Text = "C0" + ctr;
                }
                else if (ctr >= 99)
                {
                    ctr = ctr + 1;
                    textBox1.Text = "C" + ctr;
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error..." + ex.Message);
            }
}
}
}
 
Share this answer
 
v2
hii,
If you want to create unique id,just use GUID .This Will Help you.
http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx[^]

if you want to generate unique auto generated no for column in database
just use identity property for sql server.
 
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