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 AddDept.
	/// </summary>
	public partial class AddDept : 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();
			}
		}

		#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

		SqlConnection con=new SqlConnection("server=net2;integrated security=true;database=di");

		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 Button2_Click(object sender, System.EventArgs e)
		{
			txtDeptId.Text="";
			txtDeptName.Text="";
			txtPhNo.Text="";
			ddlBranchId.SelectedIndex=0;
		}

		protected void Button1_Click(object sender, System.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();
				Label5.Text="Information Posted";
			}
			catch (Exception ex)
			{
				Label5.Text=ex.Message;
			}
		}
	}
}
