using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindData();
}
private void BindData() /*this function loads the GridView with the data*/
{
try
{
string connectionString = "Data Source=MDYNYCCMTDEV;Initial Catalog=pubs;User ID=rast;Password=rast05";
string commandString = "select * from Employee1 order by EmployeeID asc";
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(commandString, con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
/*the following event creates a place holder in the GridView to enable insertion by adding a new row.When button btnAddrec is clicked,apart from data binding,the text of inherent 'Update' LinkButton is changed to 'Insert' */
protected void btnaddrec_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=MDYNYCCMTDEV;Initial Catalog=pubs;User ID=rast;Password=rast05");
SqlDataAdapter da = new SqlDataAdapter("SELECT * from Employee1", con);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr = dt.NewRow();
dt.Rows.InsertAt(dr, 0);
GridView1.EditIndex = 0;
GridView1.DataSource = dt;
GridView1.DataBind();
((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert"; /*this line tracks the GridView's 'Update' LinkButton and replaces its text to 'Insert'*/
}
/*the following event changes the GridView to editable mode so that it can enable Updation. By default edit index is -1*/
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
/*the following event handles Updation as well as Insertion based on the click status of button btnAddrec*/
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
/*if LinkButton text is 'Insert' the Insertion code is followed*/
if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
{
TextBox TextBox1;
string EmployeeName, ContactNo;
TextBox1 = (TextBox)GridView1.Rows[0].FindControl("txtEmployeeName");
EmployeeName = TextBox1.Text;
TextBox1 = (TextBox)GridView1.Rows[0].FindControl("txtContactNo");
ContactNo = TextBox1.Text;
SqlConnection con = new SqlConnection("Data Source=MDYNYCCMTDEV;Initial Catalog=pubs;User ID=rast;Password=rast05");
SqlCommand cmd = new SqlCommand("empins", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmployeeName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@ContactNo", SqlDbType.VarChar, 50);
cmd.Parameters["@EmployeeName"].Value = EmployeeName;
cmd.Parameters["@ContactNo"].Value = ContactNo;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindData();
}
/*otherwise Updation code is followed*/
else
{
Label Label1;
TextBox TextBox1;
string EmployeeID, EmployeeName, ContactNo;
Label1 = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEmployeeIDEdit");
EmployeeID = Label1.Text;
TextBox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmployeeName");
EmployeeName = TextBox1.Text;
TextBox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtContactNo");
ContactNo = TextBox1.Text;
SqlConnection con = new SqlConnection("Data Source=MDYNYCCMTDEV;Initial Catalog=pubs;User ID=rast;Password=rast05");
SqlCommand cmd = new SqlCommand("empupd", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int);
cmd.Parameters.Add("@EmployeeName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@ContactNo", SqlDbType.VarChar, 50);
cmd.Parameters["@EmployeeID"].Value = EmployeeID;
cmd.Parameters["@EmployeeName"].Value = EmployeeName;
cmd.Parameters["@ContactNo"].Value = ContactNo;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindData();
}
}
/*here is what will happen if Cancel button is clicked,GridView comes in Non Editable mode*/
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
/*Finally follows the Deletion code */
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label Label1;
string EmployeeID;
Label1 = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEmployeeID");
EmployeeID = Label1.Text;
SqlConnection con = new SqlConnection("Data Source=MDYNYCCMTDEV;Initial Catalog=pubs;User ID=rast;Password=rast05");
SqlCommand cmd = new SqlCommand("empdel", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int);
cmd.Parameters["@EmployeeID"].Value = EmployeeID;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindData();
}
}