About Me

My photo
New Delhi, Delhi, India
I am working in Infosys.

May 25, 2015

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


public partial class Employee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if  (!Page.IsPostBack)
        {
            FillDepartment();
            FillGrid();
        }
    }
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ToString());
    SqlCommand cmd = new SqlCommand();
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("Proc_Employee", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@AA", 1);
            cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text);
            cmd.Parameters.AddWithValue("@Name", txtName.Text.ToString());
            cmd.Parameters.AddWithValue("@DOB", txtDOB.Text.ToString());
            cmd.Parameters.AddWithValue("@EmailID", txtEmailID.Text.ToString());
            cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text.ToString());
            cmd.Parameters.AddWithValue("@DepetName", ddlDepartment.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@Address", txtAddress.Text.ToString());
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ClearField();
            FillGrid();
            lbl_Msg.Text = "Save successfully";
        }
        catch (Exception ex)
        {
            lbl_Msg.Text = ex.Message.ToString();
        }

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        ClearField();
    }
    private void FillDepartment()
    {
        cmd = new SqlCommand("Proc_Employee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@AA", 2);
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        con.Close();
        ddlDepartment.DataSource = ds;
        ddlDepartment.DataTextField = "DeptName";
        ddlDepartment.DataBind();


    }
    private void FillGrid()
    {
        cmd = new SqlCommand("Proc_Employee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@AA", 3);
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();




    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edit")
            {
                int RowIndex = int.Parse(e.CommandArgument.ToString());
                Label lbl_EmpID = (Label)GridView1.Rows[RowIndex].FindControl("lbl_EmpID");
                Label lbl_Name = (Label)GridView1.Rows[RowIndex].FindControl("lbl_Name");
                Label lbl_DOB = (Label)GridView1.Rows[RowIndex].FindControl("lbl_DOB");
                Label lblEmailID = (Label)GridView1.Rows[RowIndex].FindControl("lblEmailID");
                Label lbl_Mobile = (Label)GridView1.Rows[RowIndex].FindControl("lbl_Mobile");
                Label lbl_DepetName = (Label)GridView1.Rows[RowIndex].FindControl("lbl_DepetName");
                Label lbl_Address = (Label)GridView1.Rows[RowIndex].FindControl("lbl_Address");
                txtEmpID.Text = lbl_EmpID.Text;
                txtName.Text = lbl_Name.Text;
                txtDOB.Text = lbl_DOB.Text;
                txtEmailID.Text = lblEmailID.Text;
                txtMobile.Text = lbl_Mobile.Text;
                ddlDepartment.Text = lbl_DepetName.Text;
                txtAddress.Text = lbl_Address.Text;
            
              

            }
            if (e.CommandName == "Delete")
            {
                cmd = new SqlCommand("Proc_Employee", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@AA", 4);
                cmd.Parameters.AddWithValue("@EmpID", Convert.ToInt32(e.CommandArgument.ToString()));
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                lbl_Msg.Text = "Delete Sucessfully";


            }
        }
        catch (Exception ex)
        {

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView1.EditIndex = -1;
        FillGrid();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        FillGrid();
    }
    private void ClearField()
    {
        txtAddress.Text = "";
        txtDOB.Text = "";
        txtEmailID.Text = "";
        txtEmpID.Text = "";
        txtMobile.Text = "";
        txtName.Text = "";
        lbl_Msg.Text = "";
        //ddlDepartment.SelectedValue = "";
    }


    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        FillGrid();
    }
}

No comments:

Post a Comment