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:
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 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:
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:
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:
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:
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.
I hope this is helpful for you.
No comments:
Post a Comment