Thursday, 15 March 2018

# CREATE USER

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 System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    public static string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
    SqlConnection con = new SqlConnection(constr);
    public static string ses_Admin = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ses_Admin"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        ses_Admin = Session["ses_Admin"].ToString();
        lblSes.Text = "Employee Code " + " " + ses_Admin + " " + "is Logged In";
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("CreateUSer", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        con.Open();
        GenerateId();
        cmd.Parameters.AddWithValue("@Userid", txt_empid.Text);
        cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
        cmd.Parameters.AddWithValue("@Password", txtPass.Text.Trim());
        cmd.Parameters.AddWithValue("@regdate", DateTime.Now.ToString());
        cmd.Parameters.AddWithValue("@status", 'A');
        con.Close();
        reset();
        lblmessage.Text = "User Created Successfully";
    }

    public void GenerateId()
    {
        string str = "select count(*) from tbl_User";

        SqlCommand com = new SqlCommand(str, con);
        int count = Convert.ToInt16(com.ExecuteScalar()) + 1;
        txt_empid.Text = "E000" + count;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {

    }
    public void reset()
    {
        txtPass.Text = null;
        txtUserName.Text = null;
    }

    protected void btnLogin(object sender, EventArgs e)
    {
        string str = "select * from tbl_User where Userid='" + txtLoginID.Text.Trim() + "' and Password='" + txtLoginPass.Text.Trim() + "'";
        SqlDataAdapter sda = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        con.Open();
        sda.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (txtLoginID.Text == ds.Tables[0].Rows[0]["Userid"].ToString())
            {
                if (txtLoginPass.Text == ds.Tables[0].Rows[0]["Password"].ToString())
                {
                    Session["ses_Admin"] = txtLoginID.Text.Trim();
                    Response.Redirect("Employee.aspx");
                }
            }
            else
            {

                lblloginmes.Text = "Ibnvalid User Name or Password";
            }
        }
        con.Close();
    }
}


#EMPLOYEE 
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 System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    public static string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
    SqlConnection con = new SqlConnection(constr);
    public static string ses_Admin = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ses_Admin"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        ses_Admin = Session["ses_Admin"].ToString();
        lblSes.Text = "Employee Code " + " " + ses_Admin + " " + "is Logged In";

        if (!this.IsPostBack)
        {
            BindDept();
            bindEmp();
        }
    }

    public void bindEmp()
    {

        if (!this.IsPostBack)
        {

            using (SqlCommand cmd = new SqlCommand("bindEmployee"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                cgv.DataSource = cmd.ExecuteReader();
                cgv.DataBind();
                con.Close();
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("CreateEmployee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        con.Open();
        GenerateId();
        cmd.Parameters.AddWithValue("@EmpId", txt_empid.Text);
        cmd.Parameters.AddWithValue("@EmpName", txtEmpName.Text.Trim());
        cmd.Parameters.AddWithValue("@Dob", txtDob.Text.Trim());
        cmd.Parameters.AddWithValue("@Doj", txtDojoin.Text.Trim());
        cmd.Parameters.AddWithValue("@Dept", ddown_Dept.Text.Trim());
        cmd.Parameters.AddWithValue("@Salary", txtSalary.Text.Trim());
        cmd.Parameters.AddWithValue("@regdate", DateTime.Now.ToString());
        cmd.Parameters.AddWithValue("@status", 'A');
        cmd.ExecuteNonQuery();
        con.Close();
        bindEmp();
        reset();
        lblmessage.Text = "Employee Created Successfully";
    }

    public void GenerateId()
    {
        string str = "select count(*) from tbl_Employee";
        SqlCommand com = new SqlCommand(str, con);
        int count = Convert.ToInt16(com.ExecuteScalar()) + 1;
        txt_empid.Text = "E000" + count;
    }

    public void BindDept()
    {
        if (!this.IsPostBack)
        {

            using (SqlCommand cmd = new SqlCommand("select * from tbl_dept where status='A'"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                ddown_Dept.DataSource = cmd.ExecuteReader();
                ddown_Dept.DataTextField = "Dept";
                ddown_Dept.DataValueField = "Dept";
                ddown_Dept.DataBind();
                con.Close();
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        reset();
    }

    public void reset()
    {
        txt_empid.Text = null;
        txtDob.Text = null;
        txtDojoin.Text = null;
        txtEmpName.Text = null;
        txtSalary.Text = null;
        ddown_Dept.Text = null;
    }

    protected void cgv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblId = (Label)cgv.Rows[e.RowIndex].FindControl("lblId");
        string str = "select * from tbl_Employee where status='A' and id='" + lblId.Text.Trim() + "'";
        SqlDataAdapter sda = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            txtEmpName.Text = ds.Tables[0].Rows[0]["EmpName"].ToString();
            txtDob.Text = ds.Tables[0].Rows[0]["Dob"].ToString();
            txtDojoin.Text = ds.Tables[0].Rows[0]["Doj"].ToString();
            ddown_Dept.SelectedValue = ds.Tables[0].Rows[0]["Dept"].ToString();
            txtSalary.Text = ds.Tables[0].Rows[0]["Salary"].ToString();
            txtUpdateid.Text = ds.Tables[0].Rows[0]["id"].ToString();
        }
    }

    protected void cgv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblId = (Label)cgv.Rows[e.RowIndex].FindControl("lblId");
        SqlCommand com = new SqlCommand("deleteEmp",con);
        com.CommandType = CommandType.StoredProcedure; 
        com.Connection = con;
        con.Open();
        com.Parameters.AddWithValue("@id",lblId.Text.Trim());
        com.ExecuteNonQuery();
        bindEmp();
        lblmessage.Text = "Record Deleted Successfully !..";
        con.Close();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("UpdateEmployee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        con.Open();
        GenerateId();
        cmd.Parameters.AddWithValue("@id", txtUpdateid.Text.Trim());
        cmd.Parameters.AddWithValue("@EmpName", txtEmpName.Text.Trim());
        cmd.Parameters.AddWithValue("@Dob", txtDob.Text.Trim());
        cmd.Parameters.AddWithValue("@Doj", txtDojoin.Text.Trim());
        cmd.Parameters.AddWithValue("@Dept", ddown_Dept.Text.Trim());
        cmd.Parameters.AddWithValue("@Salary", txtSalary.Text.Trim());
        cmd.ExecuteNonQuery();
        con.Close();
        bindEmp();
        reset();
        lblmessage.Text = "Employee Created Successfully";
    }
   
}

#LOGIN

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 System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    public static string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
    SqlConnection con = new SqlConnection(constr);

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string str = "select * from tbl_Login where id=1";
        SqlDataAdapter sda = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        con.Open();
        sda.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (txtID.Text == ds.Tables[0].Rows[0]["LoginId"].ToString())
            {
                if (txtName.Text == ds.Tables[0].Rows[0]["LoginName"].ToString())
                {
                    Session["ses_Admin"] = txtID.Text.Trim();
                    Response.Redirect("createuser.aspx");
                }
            } 
            else
            {
                lblmessage.Text = "Ibnvalid User Name or Password";
            }
        }
        con.Close();
    }
}

#LOGOUT

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

public partial class logout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("Login.aspx");
    }
}