|
ASP.NET Register / Login Form / Reset Password / Update Profile (VB.NET , C#) |
|
|
ASP.NET Register/Login Form / Reset Password / Update Profile (VB.NET , C#) ตัวอย่าง script ของ asp.net เข้ามาจัดการระบบสมาชิก เช่น การสมัครสมาชิก (Register Form) / การทำระบบล็อกอิน (Login Form) / การทำระบบลืมรหัสผ่าน (Reset Forget Password) และ แก้ไขข้อมูลส่วนตัว (Edit Update Profile) ใช้ฐานข้อมูล SQL Server Database ผ่าน ADO.NET (NameSpace ของ System.Data.SqlClient) บน .NET Framework มีตัวอย่างให้ดูหรือดาวน์โหลดทั้งภาษา VB.NET และ C#
Screenshot
คุณสมบัติและความสามารถของตัวอย่างนี้
- ระบบสมาชิก เช่น การสมัครสมาชิก (Register Form)
- ระบบล็อกอิน (Login Form)
- ระบบลืมรหัสผ่าน (Reset Forget Password)
- แก้ไขข้อมูลส่วนตัว (Update Profile)
Database
USE [mydatabase]
GO
/****** Object: Table [dbo].[member] Script Date: 06/06/2012 11:15:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[member](
[MemberID] [int] IDENTITY(1,1) NOT NULL,
[Username] [varchar](50) NULL,
[Password] [varchar](50) NULL,
[Name] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[Country] [varchar](100) NULL,
CONSTRAINT [PK_member] PRIMARY KEY CLUSTERED
(
[MemberID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT INTO [member] VALUES ('win', 'win001', 'Win Weerachai', '[email protected]', 'Thailand');
INSERT INTO [member] VALUES ('john', 'jhon002', 'John Smith', '[email protected]', 'England');
INSERT INTO [member] VALUES ('jame', 'jame003', 'Jame Born', '[email protected]', 'United States');
INSERT INTO [member] VALUES ('chalee', 'chalee004', 'Chalee Angel', '[email protected]', 'United States');
นำ Query นี้เพื่อไปสร้าง Table
โครงสร้างฐานข้อมูล ชื่อตารางว่า member และฟิวด์ตามภาพ
พัฒนาโปรแกรมด้วย ASP.NET Web Application
โครงสร้างไฟล์
Code ทั้งหมด
- Form สำหรับ Login
frmLogin.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class frmLogin
Inherits System.Web.UI.Page
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
Dim objConn As SqlConnection
Dim strConnString As String
Dim strSQL As StringBuilder
Dim objCmd As SqlCommand
Dim intCount As Integer = 0
'*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
'*** Check Login ***'
strSQL = New StringBuilder
strSQL.Append(" SELECT COUNT(*) FROM member ")
strSQL.Append(" WHERE Username = @sUsername ")
strSQL.Append(" AND Password = @sPassword ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Me.txtUsername.Text
objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = Me.txtPassword.Text
intCount = objCmd.ExecuteScalar()
objConn.Close()
objConn = Nothing
If intCount <= 0 Then
Me.lblStatus.ForeColor = Drawing.Color.Red
Me.lblStatus.Text = "Username or Password wrong!"
Else
Session("strUsername") = Me.txtUsername.Text
Response.Redirect("frmHomeProfile.aspx")
End If
End Sub
End Class
Screenshot
ตัวอย่างหน้าจอสำหรับ Login เข้าสู่ระบบสมาชิก
- Form สำหรับสมัครสมาชิก
frmRegister.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class frmRegister
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.pnlRegister.Visible = True
Me.pnlFinish.Visible = False
End Sub
Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegister.Click
Dim objConn As SqlConnection
Dim strConnString As String
Dim strSQL As StringBuilder
Dim objCmd As SqlCommand
Dim intCount As Integer = 0
'*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
'*** Check Username exists ***'
strSQL = New StringBuilder
strSQL.Append(" SELECT COUNT(*) FROM member ")
strSQL.Append(" WHERE Username = @sUsername ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Me.txtUsername.Text
intCount = objCmd.ExecuteScalar()
If intCount >= 1 Then
Me.lblStatus.ForeColor = Drawing.Color.Red
Me.lblStatus.Text = "Username already exists!!"
Exit Sub
End If
'*** Check Email exists ***'
strSQL = New StringBuilder
strSQL.Append(" SELECT COUNT(*) FROM member ")
strSQL.Append(" WHERE Email = @sEmail ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = Me.txtEmail.Text
intCount = objCmd.ExecuteScalar()
If intCount >= 1 Then
Me.lblStatus.ForeColor = Drawing.Color.Red
Me.lblStatus.Text = "Email already exists!!"
Exit Sub
End If
'*** Insert New Member ***'
strSQL = New StringBuilder
strSQL.Append(" INSERT INTO member ")
strSQL.Append(" (Username,Password,Name,Email,Country) ")
strSQL.Append(" VALUES ")
strSQL.Append(" (@sUsername,@sPassword,@sName,@sEmail,@sCountry) ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Me.txtUsername.Text
objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = Me.txtPassword.Text
objCmd.Parameters.Add("@sName", SqlDbType.VarChar).Value = Me.txtName.Text
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = Me.txtEmail.Text
objCmd.Parameters.Add("@sCountry", SqlDbType.VarChar).Value = Me.txtCountry.Text
objCmd.ExecuteNonQuery()
objConn.Close()
objConn = Nothing
Me.pnlRegister.Visible = False
Me.pnlFinish.Visible = True
End Sub
End Class
Screenshot
หน้าจอสมัครสมาชิก
เมื่อสมัครสมาชิกเสร็จสมบูรณ์
เมื่อตรวจสอบใน Table ก็จะปรากฏข้อมูลที่ถูก Insert เข้าไป
- Form สำหรับ Reset หรือ Forgot Password
frmResetPassword.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Partial Public Class frmResetPassword
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.pnlReset.Visible = True
Me.pnlFinish.Visible = False
End Sub
Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
Dim objConn As SqlConnection
Dim strConnString As String
Dim strSQL As StringBuilder
Dim objCmd As SqlCommand
Dim dtReader As SqlDataReader
'*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
'*** Get Data ***'
strSQL = New StringBuilder
strSQL.Append(" SELECT * FROM member ")
strSQL.Append(" WHERE Username = @sUsername ")
strSQL.Append(" OR Email = @sEmail ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Me.txtUsername.Text
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = Me.txtEmail.Text
dtReader = objCmd.ExecuteReader()
If Not dtReader.HasRows Then
Me.lblStatus.ForeColor = Drawing.Color.Red
Me.lblStatus.Text = "Username or Password wrong!"
Else
dtReader.Read()
Dim myMail As New MailMessage()
myMail.To = dtReader("Email").ToString()
myMail.From = "[email protected]"
myMail.Subject = "Account Username & Password"
myMail.BodyFormat = MailFormat.Html
myMail.Body = "Username = " & dtReader("Username").ToString() & "" & _
"<br> Password = " & dtReader("Password").ToString()
'SmtpMail.SmtpServer = "192.168.0.1" '*** Mail Server ***'
SmtpMail.Send(myMail)
myMail = Nothing
Me.lblMessage.Text = "Your password has been send to mail (" & dtReader("Email").ToString() & ")"
Me.pnlReset.Visible = False
Me.pnlFinish.Visible = True
End If
dtReader.Close()
objConn.Close()
objConn = Nothing
End Sub
End Class
Screenshot
Forget รหัสผ่าน กรอก Username หรือ Password หรืออย่างใดอย่างหนึ่ง
แสดงข้อความว่าอีเมล์ได้ถุกส่ง
เมื่อกลับไปตรวจสอบอีเมล์ ก็จะมีอีเมล์ที่แจ้งรหัสผ่าน
รายละเอียด Username และ Password ที่ได้จากการ Reset Forget Password
- Form สำหรับ Home หรือ หน้าหลังหลังจาก Login ผ่าน
frmHomeProfile.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class frmHomeProfile
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'*** Check login Status ***'
If Session("strUsername") = "" Then
Response.Redirect("frmLogin.aspx")
Response.End()
End If
Call ViewDataInf()
End Sub
Protected Sub ViewDataInf()
Dim objConn As SqlConnection
Dim strConnString As String
Dim strSQL As StringBuilder
Dim objCmd As SqlCommand
Dim dtAdapter As SqlDataAdapter
Dim dt As DataTable
Dim ds As New DataSet
'*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = New StringBuilder
strSQL.Append(" SELECT * FROM member ")
strSQL.Append(" WHERE Username = @sUsername ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session("strUsername")
dtAdapter = New SqlDataAdapter
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dt = ds.Tables(0)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
If dt.Rows.Count > 0 Then
Me.lblUsername.Text = dt.Rows(0)("Username")
Me.lblPassword.Text = dt.Rows(0)("Password")
Me.lblName.Text = dt.Rows(0)("Name")
Me.lblEmail.Text = dt.Rows(0)("Email")
Me.lblCountry.Text = dt.Rows(0)("Country")
End If
End Sub
Protected Sub lnkLogout_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkLogout.Click
Session.Abandon()
Response.Redirect("frmLogin.aspx")
End Sub
End Class
Screenshot
หน้าจอนี้แสดงข้อมูลของสมาชิก หลังจากที่ Login ผ่าน
- Form สำหรับแก้ไขข้อมูลส่วนตัว หรือ Update Profile
frmUpdateProfile.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class frmUpdateProfile
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'*** Check login Status ***'
If Session("strUsername") = "" Then
Response.Redirect("frmLogin.aspx")
Response.End()
End If
Me.pnlProfile.Visible = True
Me.pnlFinish.Visible = False
If Not Page.IsPostBack() Then
ShowDataInfo()
End If
End Sub
Protected Sub ShowDataInfo()
Dim objConn As SqlConnection
Dim strConnString As String
Dim strSQL As StringBuilder
Dim objCmd As SqlCommand
Dim dtReader As SqlDataReader
'*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
'*** Get Data ***'
strSQL = New StringBuilder
strSQL.Append(" SELECT * FROM member ")
strSQL.Append(" WHERE Username = @sUsername ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session("strUsername")
dtReader = objCmd.ExecuteReader()
If dtReader.HasRows Then
dtReader.Read()
Me.lblUsername.Text = dtReader("Username").ToString()
Me.txtName.Text = dtReader("Name").ToString()
Me.txtEmail.Text = dtReader("Email").ToString()
Me.txtCountry.Text = dtReader("Country").ToString()
End If
dtReader.Close()
objConn.Close()
objConn = Nothing
End Sub
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
Dim objConn As SqlConnection
Dim strConnString As String
Dim strSQL As StringBuilder
Dim objCmd As SqlCommand
Dim intCount As Integer = 0
'*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
'*** Check Email exists ***'
strSQL = New StringBuilder
strSQL.Append(" SELECT COUNT(*) FROM member ")
strSQL.Append(" WHERE Email = @sEmail AND Username <> @sUsername")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = Me.txtEmail.Text
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session("strUsername")
intCount = objCmd.ExecuteScalar()
If intCount >= 1 Then
Me.lblStatus.ForeColor = Drawing.Color.Red
Me.lblStatus.Text = "Email already exists!!"
Exit Sub
End If
''*** Update Profile ***'
strSQL = New StringBuilder
strSQL.Append(" UPDATE member ")
strSQL.Append(" SET Password = @sPassword ")
strSQL.Append(" ,Name = @sName ")
strSQL.Append(" ,Email = @sEmail ")
strSQL.Append(" ,Country = @sCountry ")
strSQL.Append(" WHERE Username = @sUsername")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = Me.txtPassword.Text
objCmd.Parameters.Add("@sName", SqlDbType.VarChar).Value = Me.txtName.Text
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = Me.txtEmail.Text
objCmd.Parameters.Add("@sCountry", SqlDbType.VarChar).Value = Me.txtCountry.Text
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session("strUsername")
objCmd.ExecuteNonQuery()
objConn.Close()
objConn = Nothing
Me.pnlProfile.Visible = False
Me.pnlFinish.Visible = True
End Sub
End Class
Screenshot
หน้าจอสำหรับแก้ไขข้อมูลส่วนตัว
ทดสอบการแก้ไขชื่อ
ข้อมูลถูกแก้ไขเรียบร้อย
Code สำหรับภาษา C#
frmLogin.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace AspNetMember
{
public partial class frmLogin : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection objConn = null;
string strConnString = null;
StringBuilder strSQL = default(StringBuilder);
SqlCommand objCmd = null;
int intCount = 0;
//*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
//*** Check Login ***'
strSQL = new StringBuilder();
strSQL.Append(" SELECT COUNT(*) FROM member ");
strSQL.Append(" WHERE Username = @sUsername ");
strSQL.Append(" AND Password = @sPassword ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = this.txtUsername.Text;
objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = this.txtPassword.Text;
intCount = (int)objCmd.ExecuteScalar();
objConn.Close();
objConn = null;
if (intCount <= 0)
{
this.lblStatus.ForeColor = System.Drawing.Color.Red;
this.lblStatus.Text = "Username or Password wrong!";
}
else
{
Session["strUsername"] = this.txtUsername.Text;
Response.Redirect("frmHomeProfile.aspx");
}
}
}
}
frmRegister.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace AspNetMember
{
public partial class frmRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.pnlRegister.Visible = true;
this.pnlFinish.Visible = false;
}
protected void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection objConn = null;
string strConnString = null;
StringBuilder strSQL = default(StringBuilder);
SqlCommand objCmd = null;
int intCount = 0;
//*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
//*** Check Username exists ***'
strSQL = new StringBuilder();
strSQL.Append(" SELECT COUNT(*) FROM member ");
strSQL.Append(" WHERE Username = @sUsername ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = this.txtUsername.Text;
intCount = (int)objCmd.ExecuteScalar();
if (intCount >= 1)
{
this.lblStatus.ForeColor = System.Drawing.Color.Red;
this.lblStatus.Text = "Username already exists!!";
return;
}
//*** Check Email exists ***'
strSQL = new StringBuilder();
strSQL.Append(" SELECT COUNT(*) FROM member ");
strSQL.Append(" WHERE Email = @sEmail ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = this.txtEmail.Text;
intCount = (int)objCmd.ExecuteScalar();
if (intCount >= 1)
{
this.lblStatus.ForeColor = System.Drawing.Color.Red;
this.lblStatus.Text = "Email already exists!!";
return;
}
//*** Insert New Member ***'
strSQL = new StringBuilder();
strSQL.Append(" INSERT INTO member ");
strSQL.Append(" (Username,Password,Name,Email,Country) ");
strSQL.Append(" VALUES ");
strSQL.Append(" (@sUsername,@sPassword,@sName,@sEmail,@sCountry) ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = this.txtUsername.Text;
objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = this.txtPassword.Text;
objCmd.Parameters.Add("@sName", SqlDbType.VarChar).Value = this.txtName.Text;
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = this.txtEmail.Text;
objCmd.Parameters.Add("@sCountry", SqlDbType.VarChar).Value = this.txtCountry.Text;
objCmd.ExecuteNonQuery();
objConn.Close();
objConn = null;
this.pnlRegister.Visible = false;
this.pnlFinish.Visible = true;
}
}
}
frmResetPassword.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.Mail;
namespace AspNetMember
{
public partial class frmResetPassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.pnlReset.Visible = true;
this.pnlFinish.Visible = false;
}
protected void btnReset_Click(object sender, EventArgs e)
{
SqlConnection objConn = null;
string strConnString = null;
StringBuilder strSQL = default(StringBuilder);
SqlCommand objCmd = null;
SqlDataReader dtReader = null;
//*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
//*** Get Data ***'
strSQL = new StringBuilder();
strSQL.Append(" SELECT * FROM member ");
strSQL.Append(" WHERE Username = @sUsername ");
strSQL.Append(" OR Email = @sEmail ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = this.txtUsername.Text;
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = this.txtEmail.Text;
dtReader = objCmd.ExecuteReader();
if (!dtReader.HasRows)
{
this.lblStatus.ForeColor = System.Drawing.Color.Red;
this.lblStatus.Text = "Username or Password wrong!";
}
else
{
dtReader.Read();
MailMessage myMail = new MailMessage();
myMail.To = dtReader["Email"].ToString();
myMail.From = "[email protected]";
myMail.Subject = "Account Username & Password";
myMail.BodyFormat = MailFormat.Html;
myMail.Body = "Username = " + dtReader["Username"].ToString() + "" + "<br> Password = " + dtReader["Password"].ToString();
//SmtpMail.SmtpServer = "192.168.0.1" '*** Mail Server ***'
SmtpMail.Send(myMail);
myMail = null;
this.lblMessage.Text = "Your password has been send to mail (" + dtReader["Email"].ToString() + ")";
this.pnlReset.Visible = false;
this.pnlFinish.Visible = true;
}
dtReader.Close();
objConn.Close();
objConn = null;
}
}
}
frmHomeProfile.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace AspNetMember
{
public partial class frmHomeProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//*** Check login Status ***//
if (Convert.ToString(Session["strUsername"]) == "")
{
Response.Redirect("frmLogin.aspx");
Response.End();
}
ViewDataInf();
}
protected void ViewDataInf()
{
SqlConnection objConn = null;
string strConnString = null;
StringBuilder strSQL = default(StringBuilder);
SqlCommand objCmd = null;
SqlDataAdapter dtAdapter = null;
DataTable dt = null;
DataSet ds = new DataSet();
//*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
strSQL = new StringBuilder();
strSQL.Append(" SELECT * FROM member ");
strSQL.Append(" WHERE Username = @sUsername ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session["strUsername"];
dtAdapter = new SqlDataAdapter();
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dt = ds.Tables[0];
dtAdapter = null;
objConn.Close();
objConn = null;
if (dt.Rows.Count > 0)
{
this.lblUsername.Text = dt.Rows[0]["Username"].ToString();
this.lblPassword.Text = dt.Rows[0]["Password"].ToString();
this.lblName.Text = dt.Rows[0]["Name"].ToString();
this.lblEmail.Text = dt.Rows[0]["Email"].ToString();
this.lblCountry.Text = dt.Rows[0]["Country"].ToString();
}
}
protected void lnkLogout_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("frmLogin.aspx");
}
}
}
frmUpdateProfile.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace AspNetMember
{
public partial class frmUpdateProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//*** Check login Status ***'
if (Convert.ToString(Session["strUsername"]) == "")
{
Response.Redirect("frmLogin.aspx");
Response.End();
}
this.pnlProfile.Visible = true;
this.pnlFinish.Visible = false;
if (!Page.IsPostBack)
{
ShowDataInfo();
}
}
protected void ShowDataInfo()
{
SqlConnection objConn = null;
string strConnString = null;
StringBuilder strSQL = default(StringBuilder);
SqlCommand objCmd = null;
SqlDataReader dtReader = null;
//*** Open Connection ***'
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
//*** Get Data ***'
strSQL = new StringBuilder();
strSQL.Append(" SELECT * FROM member ");
strSQL.Append(" WHERE Username = @sUsername ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session["strUsername"];
dtReader = objCmd.ExecuteReader();
if (dtReader.HasRows)
{
dtReader.Read();
this.lblUsername.Text = dtReader["Username"].ToString();
this.txtName.Text = dtReader["Name"].ToString();
this.txtEmail.Text = dtReader["Email"].ToString();
this.txtCountry.Text = dtReader["Country"].ToString();
}
dtReader.Close();
objConn.Close();
objConn = null;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection objConn = null;
string strConnString = null;
StringBuilder strSQL = default(StringBuilder);
SqlCommand objCmd = null;
int intCount = 0;
//*** Open Connection ***//
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
//*** Check Email exists ***//
strSQL = new StringBuilder();
strSQL.Append(" SELECT COUNT(*) FROM member ");
strSQL.Append(" WHERE Email = @sEmail AND Username <> @sUsername");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = this.txtEmail.Text;
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session["strUsername"];
intCount = (int)objCmd.ExecuteScalar();
if (intCount >= 1)
{
this.lblStatus.ForeColor = System.Drawing.Color.Red;
this.lblStatus.Text = "Email already exists!!";
return;
}
//'*** Update Profile ***//
strSQL = new StringBuilder();
strSQL.Append(" UPDATE member ");
strSQL.Append(" SET Password = @sPassword ");
strSQL.Append(" ,Name = @sName ");
strSQL.Append(" ,Email = @sEmail ");
strSQL.Append(" ,Country = @sCountry ");
strSQL.Append(" WHERE Username = @sUsername");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = this.txtPassword.Text;
objCmd.Parameters.Add("@sName", SqlDbType.VarChar).Value = this.txtName.Text;
objCmd.Parameters.Add("@sEmail", SqlDbType.VarChar).Value = this.txtEmail.Text;
objCmd.Parameters.Add("@sCountry", SqlDbType.VarChar).Value = this.txtCountry.Text;
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = Session["strUsername"];
objCmd.ExecuteNonQuery();
objConn.Close();
objConn = null;
this.pnlProfile.Visible = false;
this.pnlFinish.Visible = true;
}
}
}
Download Code!! ทั้งหมด ทั้ง VB.NET และ C#
Download!
บทความที่เกี่ยวข้อง
Go to : ASP.NET User Register Form
Go to : ASP.NET User Authentication/Login Form
|
|
|
|
|
|
|
|
Score Rating : |
|
|
Create Date : |
2012-06-06 20:44:08 |
|
View : |
55,882 |
|
Download : |
No files
|
|
|
|
|
|
|
|