using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace NScontract.User
{
    /// <summary>
    /// UserUpdatePwd 的摘要说明。
    /// </summary>
    public partial class UserUpdatePwd : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Panel NSPn_user;
        protected System.Web.UI.WebControls.Button NSBack;
        private string NSNickName;
        SqlConnection NSconn;


        protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["NSNickName"] != null)
            {
                //获得数据库连接字符串
                string NSconnection = ConfigurationSettings.AppSettings["strconnection"];
                NSconn = new SqlConnection(NSconnection);
                //获取登录用户的登录名
                NSNickName = Session["NSNickName"].ToString();
                NSconn.Open();
                //创建查询登录用户密码的sql语句
                string NSstrsql = "select NSUserPwd from NSUser where NSNickName = '" + NSNickName + "'";
                //创建数据库的SqlCommand对象
                SqlCommand NScmd = new SqlCommand(NSstrsql, NSconn);
                //执行SqlCommand对象中数据的读取
                SqlDataReader NSsdr = NScmd.ExecuteReader();
                while (NSsdr.Read())
                {
                    //将获得的用户名密码保存到会话中
                    Session["NSUserPwd"] = NSsdr["NSUserPwd"];
                }
            }
            else
            {
                Response.Redirect("../Index.aspx");
            }

        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion



        //点击修改按钮所触发的事件
        protected void NSUpdate_ok_Click(object sender, System.EventArgs e)
        {

            //判断原密码是否为空
            if (NSOld_pwd.Text == Session["NSUserPwd"].ToString() && NSOld_pwd.Text != "")
            {
                //判断新密码和确认密码是否为空，是否相等
                if (NSNew_pwd.Text == Tbx_pwdok.Text && NSNew_pwd.Text != "" && Tbx_pwdok.Text != "")
                {
                    //获得数据库连接字符串
                    string NSconnection = ConfigurationSettings.AppSettings["strconnection"];
                    SqlConnection NSconn = new SqlConnection(NSconnection);
                    NSconn.Open();
                    //根据文本框里的内容创建修改密码的sql语句
                    string NSstrsql = "update NSUser set NSUserPwd = '" + NSNew_pwd.Text + "' where NSNickName = '" + NSNickName + "'";
                    SqlCommand NScmd = new SqlCommand(NSstrsql, NSconn);
                    try
                    {
                        //执行sql语句
                        NScmd.ExecuteNonQuery();
                        //提示修改成功
                        NSUserPwd.Text = "修改成功！";
                        Response.Redirect("NSUserInfo.aspx");

                    }
                    catch (SqlException)
                    {
                        NSUserPwd.Text = "修改有误！";
                        NSUserPwd.Visible = true;
                    }
                    NSconn.Close();
                }
                else
                {
                    NSUserPwd.Text = "确认密码不正确！";
                }
            }
            else
            {
                NSUserPwd.Text = "原密码不正确！";
            }
        }


    }
}
