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 IntranetApp
{
	/// <summary>
	/// Summary description for AddEmp.
	/// </summary>
	public partial class AddEmp : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			Label5.Text="";
			if (!IsPostBack)
			{
				setBranch();
				setEmploeeNo();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
		string gender;
		SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings.Get("con"));

		private void setEmploeeNo()
		{
			if (con.State==ConnectionState.Closed) con.Open();
			SqlCommand cmd=new SqlCommand("select isnull(max(empno)+1,1) from emp",con);
			txtEmpNo.Text=cmd.ExecuteScalar().ToString();
			con.Close();
		}

		private void setBranch()
		{
			if (con.State==ConnectionState.Closed) con.Open();
			SqlDataAdapter da=new SqlDataAdapter("select branchno,branchname from branch",con);
			DataSet ds=new DataSet();
			da.Fill(ds,"branch");
			ddlBranchId.DataSource=ds.Tables[0];
			ddlBranchId.DataTextField="branchname";
			ddlBranchId.DataValueField="branchno";
			ddlBranchId.DataBind();
			ddlBranchId.Items.Insert(0,"-Select Branch-");
			con.Close();
		}
		
		private void setDepartment()
		{
			if (con.State==ConnectionState.Closed) con.Open();
			SqlDataAdapter da=new SqlDataAdapter("select deptno,deptname from dept where branchno='" + ddlBranchId.SelectedValue + "'",con);
			DataSet ds=new DataSet();
			da.Fill(ds,"dept");
			ddlDeptNo.DataSource=ds.Tables[0];
			ddlDeptNo.DataTextField="deptname";
			ddlDeptNo.DataValueField="deptno";
			ddlDeptNo.DataBind();
			ddlDeptNo.Items.Insert(0,"-Select Department-");
			con.Close();
		}

		protected void Button2_Click(object sender, System.EventArgs e)
		{
			txtAddress.Text="";
			txtAge.Text="";
			txtEmailId.Text="";
			txtEmpName.Text="";
			txtEmpNo.Text="";
			txtPhNo.Text="";
			txtPwd.Text="";
			ddlBranchId.SelectedIndex=0;
			ddlDeptNo.SelectedIndex=0;
			ddlRole.SelectedIndex=0;
			setEmploeeNo();
		}

		protected void ddlBranchNo_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			setDepartment();
		}

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			
			if (rbMale.Checked==true) gender="Male";
			if (rbFemale.Checked==true) gender="Female";
			if (con.State==ConnectionState.Closed) con.Open();
			SqlCommand cmd=new SqlCommand("insert into emp values(" + txtEmpNo.Text + ",'" + txtEmpName.Text + "','" + txtPwd.Text + "','" + txtAge.Text  + "','" + gender + "','" + txtAddress.Text + "','" + txtPhNo.Text + "','" +txtEmailId.Text + "','" + ddlBranchId.SelectedValue + "','" + ddlDeptNo.SelectedValue + "','" + ddlRole.SelectedItem.Text + "','" + DateTime.Now.ToString("dd-MMM-yyyy") + "')",con);
			cmd.ExecuteNonQuery();
			con.Close();
			Label5.Text="Information Posted";
		}
	}
}
