ابتدا قبل از هر چیزی باید دیتابیس را در SQL بسازیم.
1-ساخت دیتابیس جهت ساخت فرم ثبت نام در ASP.NET
وارد SQL Server شده و دیتابیس خود را بسازید.
طبق تصویر زیر فیلد های خود را تنظیم کنید.
2-ساخت و طراحی فرم عضویت
01
02
03
04
05
06
07
08
09
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
< head runat = "server" > < title >Sample Registration Page</ title > < style type = "text/css" > .style1 { width: 100%; } </ style > </ head > < body > < form id = "form1" runat = "server" > < div > < table class = "style1" > < tr > < td >Full Name:</ td > < td > < asp:TextBox ID = "TxtName runat=" server"></ asp:TextBox > </ td > </ tr > < tr > < td >Username:</ td > < td > < asp:TextBox ID = "TxtUserName" runat = "server" ></ asp:TextBox > </ td > </ tr > < tr > < td >Password:</ td > < td > < asp:TextBox ID = "TxtPassword" runat = "server" TextMode = "Password" ></ asp:TextBox > </ td > </ tr > < tr > < td >Re Password:</ td > < td > < asp:TextBox ID = "TxtRePassword" runat = "server" TextMode = "Password" ></ asp:TextBox > </ td > </ tr > < tr > < td >Address:</ td > < td > < asp:TextBox ID = "TxtAddress" runat = "server" ></ asp:TextBox > </ td > </ tr > < tr > < td >Age:</ td > < td > < asp:TextBox ID = "TxtAge" runat = "server" ></ asp:TextBox > </ td > </ tr > < tr > < td >Gender:</ td > < td > < asp:DropDownList ID = "DropDownList1" runat = "server" AppendDataBoundItems = "true" > < asp:ListItem Value = "-1" >Select</ asp:ListItem > < asp:ListItem >Male</ asp:ListItem > < asp:ListItem >Female</ asp:ListItem > </ asp:DropDownList > </ td > </ tr > </ table > </ div > < asp:Button ID = "Button1" runat = "server" Text = "Save" onclick = "Button1_Click" /> </ form > </ body > </ html > |
3-تنظیمات WebConfig و کانکشن استرینگ
در Webconfig شما نمونه کد زیر قرار دارد که در زیر مشاهده میکنید.
01
02
03
04
05
06
|
<connectionStrings> <add name= "MyConsString" connectionString="Data Source=WPHVD185022-9O0; Initial Catalog=MyDatabase; Integrated Security=SSPI;" providerName= "System.Data.SqlClient" /> </connectionStrings> |
4- فراخوانی ConnectionString
در کد زیر فراخوانی رشته اتصال در فایل Web.Config تنظیم شده است.
01
02
03
04
|
public string GetConnectionString(){ return System.Configuration.ConfigurationManager.ConnectionStrings[ "MyConsString" ].ConnectionString; } |
5-نوشتن کد جهت ارسال اطلاعات به دیتابیس
01
02
03
04
05
06
07
08
09
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
42
43
44
45
|
private void ExecuteInsert( string name, string username, string password, string gender, string age, string address) { SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO tblRegistration (Name, UserName, Password, Gender, Age, Address) VALUES " + " (@Name,@UserName,@Password,@Gender,@Age,@Address)" ; try { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter[] param = new SqlParameter[6]; //param[0] = new SqlParameter("@id", SqlDbType.Int, 20); param[0] = new SqlParameter( "@Name" , SqlDbType.VarChar, 50); param[1] = new SqlParameter( "@UserName" , SqlDbType.VarChar, 50); param[2] = new SqlParameter( "@Password" , SqlDbType.VarChar, 50); param[3] = new SqlParameter( "@Gender" , SqlDbType.Char, 10); param[4] = new SqlParameter( "@Age" , SqlDbType.Int, 100); param[5] = new SqlParameter( "@Address" , SqlDbType.VarChar, 50); param[0].Value = name; param[1].Value = username; param[2].Value = password; param[3].Value = gender; param[4].Value = age; param[5].Value = address; for ( int i = 0; i < param.Length; i++) { cmd.Parameters.Add(param[i]); } cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:" ; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } } |
6-ثبت اطلاعات و فراخوانی متد
کد زیر علاوه بر ثبت اطلاعات چک میکنید که اطلاعات به درستی وارد شده باشد و همچنین مشابه بودن پسورد ها.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
protected void Button1_Click( object sender, EventArgs e){ if (TxtPassword.Text == TxtRePassword.Text) { //call the method to execute insert to the database ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, DropDownList1.SelectedItem.Text, TxtAge.Text, TxtAddress.Text); Response.Write( "Record was successfully added!" ); ClearControls(Page); } else { Response.Write( "Password did not match" ); TxtPassword.Focus(); } } |
بسیار خوب کار تمام است ! فیلم آموزشی نیز به زودی براتون آماده خواهیم کرد.