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.Manager
{
    /// <summary>
    /// AddDep 的摘要说明。
    /// </summary>
    public partial class AddDep : System.Web.UI.Page
    {

        SqlConnection NSconn;

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (Session["NSNickName"] != null)
            {
                //获得数据库连接字符串
                string NSconnection = ConfigurationSettings.AppSettings["strconnection"];
                //创建数据库的SqlConnection
                NSconn = new SqlConnection(NSconnection);
            }
            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 NSDe_add_Click(object sender, System.EventArgs e)
        {

            if (NSDeName.Text == "")
            {
                NSDe_warn.Text = "名称不能为空！";
            }
            else if (NSDeMaster.Text == "")
            {
                NSDe_warn.Text = "负责人不能为空！";
            }
            else
            {
                string name = this.NSDeName.Text.Trim();
                string master = this.NSDeMaster.Text.Trim();
                string info = this.NSDeInfo.Text.Trim();
                string sqlstr = "insert into NSDep(NSDepName,NSDepMaster,NSDepAdd) values('" + name + "','" + master + "','" + info + "')";
                SqlCommand cmd = new SqlCommand(sqlstr,NSconn);
                try
                {
                    NSconn.Open();
                    int count = Convert.ToInt32(cmd.ExecuteNonQuery());
                    if (count > 0)
                    {
                        Response.Write("<script>alert('添加成功！')</script>");
                    }
                }
                catch (SqlException ex)
                {
                    string s = ex.Message;
                    NSDe_warn.Text = "添加失败！错误信息：" + s;
                    NSDe_warn.Style["color"] = "red";
                }
                finally
                {
                    NSconn.Close();
                    NSconn.Dispose();
                }
            }

        }
        //重填连锁店信息
        protected void NSDe_reset_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("NSAddDep.aspx");
        }
    }
}
