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 AddDept : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            setBranch();
        }
    }
    SqlConnection con = new SqlConnection("server=DESKTOP-C3BUFVQ\\SQLEXPRESS;integrated security=true;database=E2M");

    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();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (con.State == ConnectionState.Closed) con.Open();
            SqlCommand cmd = new SqlCommand("insert into dept values('" + txtdeptId.Text + "','" + txtdeptName.Text + "','" + txtPhNo.Text + "','" + ddlBranchId.SelectedValue + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            lbltext.Text = "Information Posted";
        }
        catch (Exception ex)
        {
            lbltext.Text = ex.Message;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        txtdeptId.Text = "";
        txtdeptName.Text = "";
        txtPhNo.Text = "";
        ddlBranchId.SelectedIndex = 0;
    }
}
