 |
|
หน้า สมัครสมาชิกครับ
Signup.apsx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="main.aspx.vb" Inherits="main" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblcbase" runat="server" Text="สถานะการเชื่อต่อ Database : " />  
<asp:label id="lblbase" runat="server" /> <br />
<asp:Label ID="lblname" runat="server" Text="Name : " />   
<asp:TextBox ID="txtname" runat="server" /> <br />
<asp:Label ID="lblpass" runat="server" Text="Password"/>   
<asp:TextBox ID="txtpass" runat="server" TextMode="Password" /> <br />
<asp:Label ID="lblcpass" runat="server" Text="Check Password" />   
<asp:TextBox ID="txtcpass" runat="server" TextMode ="Password" /> <br />
<asp:Button ID="btnsubmit" runat="server" Text =" OK " />
<asp:Button ID="btnreset" runat="server" Text =" Reset " />
</div>
</form>
</body>
</html>
Signup.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Class main
Inherits System.Web.UI.Page
Dim objconn As SqlConnection
Dim objcmd As SqlCommand
Dim strconn As String
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
If txtname.Text = "" Then
MsgBox("Insert Your Username")
ElseIf txtpass.Text <> txtcpass.Text Then
MsgBox("Password not Math")
ElseIf txtpass.Text = "" And txtcpass.Text = "" Then
MsgBox("Insert Password")
Else
Session("name") = txtname.Text
Session("pass") = txtpass.Text
Server.Transfer("addnewuser.aspx")
End If
End Sub
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
strconn = "Server=localhost; Uid=sa; PASSWORD=1234; database=test;"
objconn = New SqlConnection(strconn)
objconn.Open()
If objconn.State = ConnectionState.Open Then
lblbase.Text = "สามารถติดต่อฐานข้อมูลได้"
Else
lblbase.Text = "ไม่สามารถติดต่อฐานข้อมูลได้"
End If
objconn.Close()
objconn = Nothing
End Sub
End Class
หน้าส่งค่าสมาชิกไปเก็บค่า
addnewuser.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="addnewuser.aspx.vb" Inherits="addnewuser" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblconn" runat="server" /><br />
<asp:Label ID="lblname" runat="server" Text="Name : " />
<asp:TextBox ID="txtname" runat="server" /><br />
<asp:Label ID="lblpass" runat="server" Text="Password : " />
<asp:TextBox ID="txtpass" runat="server" /><br />
<br />
<asp:Button ID="btninsert" runat="server" Text="Insert Data" />
<br /><br /><br />
<asp:Button ID="btndel" runat="server" Text=" Del User " />
<asp:Button ID="btnupdate" runat="server" Text=" Edit User" />
</div>
</form>
</body>
</html>
addnewuser.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Class addnewuser
Inherits System.Web.UI.Page
Dim objconn As SqlConnection
Dim objcmd As SqlCommand
Dim strconn As String
Dim name As String
Dim pass As String
Dim strsql As String
Dim check As SqlDataReader
Dim num As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btndel.Enabled = False
btnupdate.Enabled = False
strconn = "Server=localhsot; UID=sa; PASSWORD=1234; database=test;"
objconn = New SqlConnection(strconn)
objconn.Open()
If objconn.State = ConnectionState.Open Then
lblconn.Text = ("DATABSE Connection Success !")
Else
lblconn.Text = "DATABASE Connection Lose !"
End If
txtname.Text = Session("name")
txtpass.Text = Session("pass")
txtname.Enabled = False
txtpass.Enabled = False
End Sub
Protected Sub btninsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btninsert.Click
name = Session("name")
pass = Session("pass")
strsql = "insert into testtab1 (col1,col2) values ('" & name & "','" & pass & "') "
objcmd = New SqlCommand(strsql, objconn)
check = objcmd.ExecuteReader()
'While check.Read
' num = check("เพิ่มค่าเรียบร้อยแล้ว")
' MsgBox(num)
'End While
objconn.Close()
objconn = Nothing
Session("name") = name
btndel.Enabled = True
btnupdate.Enabled = True
btninsert.Enabled = False
End Sub
Protected Sub btndel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndel.Click
Server.Transfer("deluser.aspx")
End Sub
Protected Sub btnupdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Server.Transfer("edituser.aspx")
End Sub
End Class
จากนั้นเมื่อ Insert เสร็จนะครับ ระบบจะเก็บข้อมูลลงไปที่ database ครับ
ผมสร้างไฟล์ ล็อกอินต่อ
login.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="login.aspx.vb" Inherits="login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<p align="center">
<tr>
<td style="width: 158px" align="left" valign="top">
<asp:Label ID="lblName" runat="server" Text="Username : " /><br />
<asp:TextBox ID="txtUsername" runat="server" />
</td>
</tr>
<tr>
<td style="width: 158px" align="left" valign="top">
<asp:Label ID="lblPass" runat="server" Text ="Password : " /> <br />
<asp:TextBox id="txtPassword" runat="server" Textmode="Password" />
</td>
</tr>
<tr>
<td style="width: 158px" align="left" valign="top">
<p align="center"><asp:Button ID="btnLogin" runat="server" Text ="Login" /></p>
</td>
</tr>
</p>
</table>
<br />
</div>
</form>
</body>
</html>
login.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Class login
Inherits System.Web.UI.Page
Dim objconn As SqlConnection
Dim objcmd As SqlCommand
Dim strConn As String
Dim username As String
Dim password As String
Dim strsql As String
Dim check As SqlDataReader
Dim num As Integer
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
strConn = "Server=localhost; Uid=sa; PASSWORD=1234; Database=test;"
objconn = New SqlConnection(strConn)
objconn.Open()
If objconn.State = ConnectionState.Open Then
Response.Write("Connection Pass !")
'txtUsername.Enabled = False
Else
Response.Write("Connection Lose !")
'txtPassword.Enabled = False
End If
End Sub
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
username = txtUsername.Text
password = txtPassword.Text
strsql = "select * from testtab1 where col1 = '" & username & "' and col2 = '" & password & "'"
objcmd = New SqlCommand(strsql, objconn)
'objcmd.ExecuteNonQuery()
'Response.Write(objcmd.ExecuteNonQuery())
check = objcmd.ExecuteReader()
'ล่างตรงนี้แหละครับที่มันเกิดปัญหา
While check.Read()
If (username = check("col1").ToString) And (password = check("col2").ToString) Then
MsgBox("Username And Password Right")
ElseIf (username <> check("col1").ToString) And (password <> check("col2").ToString) Then
MsgBox("Username And Password Fail")
End If
End While
check.Close()
objconn.Close()
End Sub
End Class
ผมได้สร้าง page login.aspx เอาไว้ใช้ในการล็อกอินเข้าสู่ระบบ แต่ทีนี้มันมีปัญหาที่ว่า มันเออเร่อที่ว่า ถ้าใส่ username ถูกต้องมันจะ โชว์ว่า username ถูกต้อง แต่มันไม่ยอมวิ่งไปตามลิ้งที่ผมกำหนดครับ และถ้าใส่ผิดมันจะไม่แจ้งอะไรเลย ผมลอง Debug ดูแล้วมันไม่ได้เข้าไปที่ else ที่ผมเขียนไว้เลยอยากทราบว่าทำอย่างไร มันถึงจะวิ่งเข้า else หรือว่าท่านใด มี code ที่รวบรัดและใช้งานง่ายกว่านี้รบกวน ขอเอาไว้เป้นตัวอย่างด้วยนะครับ ขอบคุณมากครับ
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-04-20 15:19:30 |
By :
thecroy |
View :
1582 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |