using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Department : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

//to populate the country name in dropdown
        if (!IsPostBack)
        {

            SqlConnection deptConnection = new SqlConnection(“Server=.;Database=Login;trusted_connection=true”);
            SqlCommand deptCommand = new SqlCommand(“Select * from Country”, deptConnection);           //Country is a table
            deptConnection.Open();
            SqlDataReader reader = deptCommand.ExecuteReader();
            DLocationDropDownList.DataSource = reader;
            DLocationDropDownList.DataTextField = “CountryName”;
            DLocationDropDownList.DataValueField = “CountryId”;
            DLocationDropDownList.DataBind();

        }
        

    }
    protected void AddDeptButton_Click(object sender, EventArgs e)
    {
        SqlConnection deptConnection = new SqlConnection(“Server=.;Database=Login;trusted_connection=true”);
        SqlCommand addcmd = new SqlCommand(“AddDept”, deptConnection);
        addcmd.CommandType = CommandType.StoredProcedure;
        deptConnection.Open();
        SqlParameter returnParameter = addcmd.Parameters.Add(“@result”, SqlDbType.Int);
        returnParameter.Direction = ParameterDirection.ReturnValue;
        addcmd.Parameters.AddWithValue(“@DepartmenttName”, DNameTextBox.Text);
        addcmd.Parameters.AddWithValue(“@CountryId”, DLocationDropDownList.SelectedValue);
        addcmd.Parameters.AddWithValue(“@Discription”, DiscriptiomnTextBox.Text);
      
        addcmd.ExecuteNonQuery();
        ResultLabel.Text = addcmd.Parameters[“@result”].Value.ToString();
   
    }
    protected void CancelButton_Click(object sender, EventArgs e)
    {
        Response.Redirect(“Department.aspx”);
    }

    protected void DLocationDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

Advertisements