Tuesday 11 June 2013

How to Generate Unique Number and Store it to Database

How to Generate Unique Number and Store it to Database

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:
Description: http://3.bp.blogspot.com/-IfxbsC9RT8Y/UX6vk9_tmpI/AAAAAAAAA40/VKvOtXOg-V8/s1600/home.PNG

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:
Description: http://1.bp.blogspot.com/-NSm6uQPaqOY/UX6xMIdz9BI/AAAAAAAAA5A/svWvKLFzGig/s640/student.PNG

Step 3: Now Double click on Submit Button and write the following codes:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 = newSqlConnection(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:
Description: http://4.bp.blogspot.com/-LmrJeLKEWGA/UX6y3b2CEmI/AAAAAAAAA5Q/vDHI71UDzCU/s1600/uninque.PNG

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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:
Description: http://1.bp.blogspot.com/-Kq3icra9Lkw/UX60aF_R2DI/AAAAAAAAA5g/wSOLyWZeJgU/s1600/verify.PNG

Step 7: Now Double Click on Verify Button and write the following codes which is given below:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 = newSqlConnection(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:
Description: http://4.bp.blogspot.com/-cdqDvWvSV-A/UX64wBO-XwI/AAAAAAAAA50/ADScwdv4g1g/s1600/randompage.PNG


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

Description: http://2.bp.blogspot.com/-1lDGwQ6XI04/UX66dutdvNI/AAAAAAAAA6A/B8cPRr8N1_Q/s1600/generateno.PNG

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

Step 10: Now Enter the Unique Number and click Verify Button.
see output:
Description: http://2.bp.blogspot.com/-FnCpKR0DpcU/UX68B4qlAuI/AAAAAAAAA6Q/xG-II5kEV3U/s1600/VERIFICATION.PNG

Step 11: Now open Database(.mdf).you will see Ajit is added in Student table with an unique number.
see it:
Description: http://1.bp.blogspot.com/-9iWKE4EDbrc/UX69B8VdnaI/AAAAAAAAA6g/ofxRbwCrv3U/s1600/uniquestudent.PNG

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.
I hope this is helpful for you.


No comments:

Post a Comment