How to Generate Unique Number and Store it to Database

By
Hi Friends! Here i am going to make an interactive application for you.In this application i will tell you how to generate Unique Number and store it to Database(.mdf). This is very good application which is mostly used in many websites.you can also find exact person name with help of this Unique Number form Database(.mdf).In this application i have used session object to transfer values from one page to another page. In this application i have generated 12 digit Unique Number.You can change Number of Digit on the basis of your requirement.
There are some steps please follow them.
Step 1: First open your visual studio-->File-->New-->Website-->ASP.NET Empty Website-->click OK.-->Now make a web form like this which is given below:

see it:

Note -> you can easily get connection string from SqlDataSource control after configuring this.
Step 2: Now go Solution Explorer-->Add New Item-->Sql Server Database-->click Add.
Now Double click on Database(.mdf) and Create New table(student)which is shown below:
see it:

Step 3: Now Double click on Submit Button and write the following codes:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    public String characters = "abcdeCDEfghijkzMABFHIJKLNOlmnopqrPQRSTstuvwxyUVWXYZ";

    public string UniqueNumber()
    {
        Random unique1 = new Random();
        string s = "IN";
        int unique;
        int n = 0;
        while (n < 10)
        {
            if (n % 2 == 0)
            {
                s += unique1.Next(10).ToString();

            }
            else
            {
                unique = unique1.Next(52);
                if (unique < this.characters.Length)
                    s = String.Concat(s, this.characters[unique]);
            }
            Label2.Text = s.ToString();
            n++;
        }
        return s;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String p = UniqueNumber();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        String str = "insert into student(uniqueno,name,age,mobile) values( '" + Label2.Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        con.Close();
        Session["id"] = Label2.Text;
        Session["name"] = TextBox1.Text;
        try
        {
            Response.Redirect("uniqueno.aspx");
        }
        catch
        {
            Label2.Text = "Please Enter correct details.....";

            this.Label2.ForeColor = Color.Red;
        }

    }

}


Step 4: Now create a New web Form(unique.aspx) Which is given below:
see it:

Note->In this application (* )represents the Label control.
Step 5: Now Double click on page , Link Button and write the following codes which is given below:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class uniqueno : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["id"].ToString();
        Label2.Text = Session["name"].ToString();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("verify.aspx");
    }
}


Step 6: Now create a New web Form(verify.aspx) lik this Which is given below:
see it:

Step 7: Now Double Click on Verify Button and write the following codes which is given below:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class verify : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select* from student where uniqueno='" + TextBox1.Text + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Label1.Text = "This record is present in database records";
            Label3.Text = dr[1].ToString();
        }
        else
        {
            Label1.Text = "This record is not present in database records";
            Label3.Text = "Not Available";
        }
    }
}
Step 8: Now  Run the Program(press F5 ) and Enter the TextBox Values.
see it:


Step 9: Now Click the Submit Button.see output.



Note-->Please remember your unique Number and Press click here Button


Step 10: Now Enter the Unique Number and click Verify Button.
see output:

Step 11: Now open Database(.mdf).you will see Ajit is added in Student table with an unique number.
see it:

Note-> In this application i have used session object for transferring values to one page to another page.
If you want to run this application without sql connection problem then follow these two steps;
  1. First download this application form bottom and open this application to your visual studio 2010.
  2. Now go Tools -->Options -->Database Tools -->Data connections -->Remove Sql Server Instance Name(blank for default) from right hand side-->click OK.
For More:
I hope this is helpful for you.
 Download whole Attached  application.
       Download

6 comments:

  1. i have written all statement in my prectical but Now iam facing a problem like as
    String or binary data would be truncated.
    The statement has been terminated.
    and
    at this place-

    Line 50: String str = "insert into student(uniqueno,name,age,mobile) values( '" + Label1.Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
    Line 51: SqlCommand cmd = new SqlCommand(str, con);
    Line 52: cmd.ExecuteNonQuery();
    Line 53: con.Close();
    Line 54: Session["id"] = Label1.Text;

    ReplyDelete
    Replies
    1. in step 3 , Label2.Text = s.ToString();it stores the random number to Label2.Text control. first store it as string format.then it may not give error.....

      Delete
  2. This website is very help for me...

    ReplyDelete
  3. This is really very informative and useful website

    ReplyDelete
  4. can you explain the unique random number operation in this code
    i can't understand how you store random variable store in databse????
    please explain me

    ReplyDelete
    Replies
    1. In above code you can see.I have stored 10 characters in Lebel2.Text like this.
      Label2.Text = s.ToString();
      after that i have stored this Label2.Text value in database. you can generate more than 10 character also according to your requirements.

      Delete

Powered by Blogger.