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: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFI9VfulZeooG0hwRRtmwTpu39otnq9hjslRIHu2G7SZInUO7BXAwSlsUHguPTl17FQJP1J2LmFby_t2uGlOX8TgeEzn_-PnybUMV6ryOzqlVVgt2K89_83zf3bT3GMHaZ59WbYf_b_nRC/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: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjarQ9Ks18BBHd9kFbnSQp6Xh4AgypDfP-bswPRLKtJ3ZqbstkNTJBw9vO06JHmU1-gKBwZ9hiZzUgU-ZF5qfqOyw_Z-Xzdg7Ut7sqlPG876yxskLN12YycV6OFmR-cUGvPfcIAIPJmFVwe/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: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6lVA6u08cw9Gnzbt2ywlYn1YgVg91yU5kjtmuzkWVRk7VMVk04zXMUWoOOxTtIOFNjXi6x9LZfxetqsbUftic1L2tqbMOYgrcUiEMpU3tHtbjJP5LC_6tNOFBGtequddGlvKKNM7iDnD1/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: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVFRIW8NPDve9a88ORV4ckP2k8imh62tOX7q28u0Ji7eq9PkS-2O625pwmxQp3Sxi2TndbJJYGZr6KnhUARTKuUriOofix7Pjxe3PhDcZtLe7lpnx2t3ZS6ptYkXP-b6mn-D-L_Sbr22LA/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: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNs6nIcDywJ_4Qo333ibB63v9eMubUCbd90ASuF3ytm5iD1DUaRDQ1fW2HF0wW8BzvxXLO72JmRcURo3MdiRl7hCXZeLGEoG82B1Y6Z4hm6By4OQbnCkHqiomjx0WiH2TytkaGDqnQFoD7/s1600/randompage.PNG


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

Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIHg0AGwjMcBFoU6XW-gpVF_FFWwZjtAiT0tYOP9Kv18fV-qSvZCNt1sKLh4W6lvZdhNBLKOfzY5nv9iVTXrN7_RLT5zl_EKodZrCY7niLrEpSCCSX5gpdxuahHX0M4igiUDa28g1z_m3H/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: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj__YUYevdLEr18aeJ9jEpCdks2e8ZebMkUXLCQbp1BuEwZRurVK2KsbXSV9NlGVKbGtSI44Zq0DKgZc4INKhQqSjWCkJM0AC28u4HoSKTgZwpkfAitR6vigFnb4recTL6rIVO0wY9EN2eJ/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: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKpM6PxV45VzAQ-p9xN-9cDeb54q-KwhJCIhIZsFvVmEagEPnP6QL52s2PyNHxj31MgtNyuZF4HkQBwC9eE-CFY-72i0g0V7J4k2tCzfydK_uwbWcHGgKLf8f7Bw_f_R2QBHvg5rf1sLmP/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