using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class AddEmp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            setBranch();
            setEmploeeNo();
        }
    }
    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 Button1_Click(object sender, 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();
        lbltext.Text = "Information Posted";
    }
    protected void Button2_Click(object sender, 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, EventArgs e)
    {
        setDepartment();
    }
}
