Saturday, May 31, 2014

Code To Add New Users and Get Data fro drop down List

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

public partial class AddEmployee : System.Web.UI.Page
{
    Employee _employee;
    DataTable _dtDepartment , _dtDsignation;
    DataSet _ds;
    DBWrapper _dbwrapper;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtState.Text = Request.QueryString.Get("state");
            txtEmpId.Text = Request.QueryString.Get("EmpId");

            if (txtState.Text.Equals("edit"))
            {
                lblTitle.Text = "Edit Employee";
                btnSubmit.Text = "Update";
                LoadEmployee();
            }
            else if (txtState.Text.Equals("view"))
            {
                lblTitle.Text = "Employee Details";
                btnSubmit.Visible = false;

                DisableControl();
           
             
                LoadEmployee();
         
         
            }

            loadDesignation();
            loadDepartment();

   
        }
    }

    private void loadDesignation()
    {
        _dbwrapper = new DBWrapper();
        _ds = new DataSet();
        _dtDsignation = _ds.Tables.Add("tblDesignations");
        _dtDsignation = _dbwrapper.GetDataSet("SELECT DesgID ,Designation FROM tblDesignations").Tables[0];
        ddlDesignations.DataSource = _dtDsignation;
        ddlDesignations.DataTextField = "Designation";
        ddlDesignations.DataValueField = "DesgID";
        ddlDesignations.DataBind();
        ddlDesignations.Items.Insert(0, new ListItem("---Select---", "0"));

     
    }

    private void loadDepartment()
    {
        _dbwrapper = new DBWrapper();
        _ds = new DataSet();
        _dtDepartment = _ds.Tables.Add("tblDepartment");
        _dtDepartment = _dbwrapper.GetDataSet("SELECT DeptID,Departments FROM tblDepartment").Tables[0];
        ddlDepartments.DataSource = _dtDepartment;  
        ddlDepartments.DataTextField = "Departments";
        ddlDepartments.DataValueField = "DeptID";
        ddlDepartments.DataBind();
        ddlDepartments.Items.Insert(0, new ListItem("---Select---", "0"));
   

     

     
    }

    private void DisableControl()
    {
        txtEmpName.ReadOnly = true;
        txtAddress.ReadOnly = true;
        txtEmail.ReadOnly = true;
    }

    private void LoadEmployee()
    {
        _employee = new Employee();
        _employee=_employee.GetEmployee(Convert.ToInt32(txtEmpId.Text));

        txtEmpName.Text = _employee.Name;
        txtAddress.Text = _employee.Address;
        txtEmail.Text = _employee.Email;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (btnSubmit.Text.Equals("Update"))
        {
            _employee = new Employee();
            _employee = _employee.GetEmployee(Convert.ToInt32(txtEmpId.Text));
            _employee.Name = txtEmpName.Text;
            _employee.Address = txtAddress.Text;
            _employee.Email = txtEmail.Text;

            _employee.Update();
        }
        else if (btnSubmit.Text.Equals("view"))
        {
            _employee = new Employee();
            _employee.Name = txtEmpName.Text;
            _employee.Address = txtAddress.Text;
            _employee.Email = txtEmail.Text;
         
     
     
        }
        else
        {
            _employee = new Employee();
            _employee.Name = txtEmpName.Text;
            _employee.Address = txtAddress.Text;
            _employee.Email = txtEmail.Text;
            _employee.DesgID = int.Parse(ddlDesignations.SelectedValue);
            _employee.DeptID =int.Parse(ddlDepartments.SelectedValue);
            _employee.Insert();

        }
        Response.Redirect("ManageEmployees.aspx");



    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ManageEmployees.aspx");
    }
}

0 comments:

Post a Comment