001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.ComponentModel;
004.
using
System.Data;
005.
using
System.Drawing;
006.
using
System.Linq;
007.
using
System.Text;
008.
using
System.Windows.Forms;
009.
using
System.Data.SqlClient;
010.
011.
namespace
NewProjectClient
012.
{
013.
public
partial
class
ChangePassword : Form
014.
{
015.
public
ChangePassword()
016.
{
017.
InitializeComponent();
018.
}
019.
020.
private
void
button2_Click(
object
sender, EventArgs e)
021.
{
022.
Showdata f =
new
Showdata();
023.
f.Show();
024.
this
.Hide();
025.
}
026.
private
void
ClearPassword()
027.
{
028.
txtID.Text =
""
;
029.
txtPasswd.Text =
""
;
030.
txtPasswd1.Text =
""
;
031.
txtPasswd2.Text =
""
;
032.
txtID.Focus();
033.
}
034.
private
void
button1_Click(
object
sender, EventArgs e)
035.
{
036.
string
StrConn =
"Data Source=.\\SQLExpress;Initial Catalog=Server;Integrated Security=True"
;
037.
if
(txtID.Text.Trim() ==
""
|| txtPasswd.Text.Trim() ==
""
|| txtPasswd1.Text.Trim() ==
""
|| txtPasswd2.Text.Trim() ==
""
)
038.
{
039.
MessageBox.Show(
"กรุณาป้อนข้อมูลให้ครบด้วยคะ"
,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
040.
ClearPassword();
041.
return
;
042.
}
043.
if
(txtPasswd1.Text.Length < 4 || txtPasswd2.Text.Length < 4)
044.
{
045.
MessageBox.Show(
"รหัสผ่านต้องมีจำนวนตัวอักษรระหว่าง 4-16 ตัวอักษร !!!"
,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
046.
ClearPassword();
047.
return
;
048.
}
049.
if
(txtPasswd.Text.Trim() == txtPasswd1.Text.Trim())
050.
{
051.
MessageBox.Show(
"รหัสผ่านเก่ากับใหม่ ห้ามเหมือนกัน !!!"
,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
052.
ClearPassword();
053.
return
;
054.
}
055.
056.
if
(txtPasswd1.Text.Trim() != txtPasswd2.Text.Trim())
057.
{
058.
MessageBox.Show(
"คุณป้อนรหัสรหัสผ่านไม่เหมือนกัน กรุณาป้อนใหม่ !!!"
,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
059.
ClearPassword();
060.
return
;
061.
}
062.
if
(MessageBox.Show(
"คุณต้องการแก้ไขรหัสผ่านใช่หรือไม่?"
,
"คำยืนยัน"
, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
063.
{
064.
065.
string
qry = @
"select * from user1 "
;
066.
string
upd = @
"update user1 set Password = @Password where IDUser = @IDUser"
;
067.
SqlConnection Conn =
new
SqlConnection(StrConn);
068.
Conn.Open();
069.
try
070.
{
071.
SqlDataAdapter da =
new
SqlDataAdapter();
072.
da.SelectCommand =
new
SqlCommand(qry, Conn);
073.
074.
DataSet ds =
new
DataSet();
075.
da.Fill(ds,
"user1"
);
076.
077.
DataTable dt = ds.Tables[
"user1"
];
078.
079.
080.
int
countrows = ds.Tables[0].Rows.Count;
081.
int
countcolumns = ds.Tables[0].Columns.Count;
082.
083.
for
(
int
y = 0; y < countrows; y++)
084.
{
085.
string
id = ds.Tables[0].Rows[y].ToString();
086.
txtID.Text = id.ToString();
087.
dt.Rows[y][
"Password"
] = txtPasswd1.Text.Trim();
088.
089.
}
090.
091.
092.
093.
foreach
(DataRow row
in
dt.Rows)
094.
{
095.
Console.WriteLine(
096.
"{0}"
,
097.
row[
"Password"
].ToString().PadRight(15));
098.
099.
}
100.
101.
102.
SqlCommand cmd =
new
SqlCommand(upd, Conn);
103.
cmd.Parameters.Add(
"@Password"
, SqlDbType.NVarChar, 15,
"Password"
).Value = txtPasswd.Text.Trim();
104.
SqlParameter parm = cmd.Parameters.Add(
"@IDUser"
, SqlDbType.NVarChar, 15,
"IDUser"
);
105.
parm.SourceVersion = DataRowVersion.Original;
106.
da.UpdateCommand = cmd;
107.
da.Update(ds,
"user1"
);
108.
109.
110.
Showdata f =
new
Showdata();
111.
f.Show();
112.
this
.Hide();
113.
}
114.
catch
(Exception ex)
115.
{
116.
MessageBox.Show(
"ไม่สามารถแก้ไขรหัสผ่านได้เนื่องจาก "
+ ex.Message,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
117.
118.
}
119.
finally
120.
{
121.
Conn.Close();
122.
}
123.
124.
125.
}
126.
127.
}
128.
}
129.
}