Refer below code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowData();
}
}
protected void ShowData()
{
OleDbConnection connn = new OleDbConnection(@"Provider=Microsoft.ACE.OleDb.12.0;Data Source=C:\Users\sivaranjani\Documents\Database6.accdb");
connn.Open();
OleDbCommand cmd = new OleDbCommand("Select DID,DEPID,DEPNAME,MANAGERNAME from tab", connn);
OleDbDataReader rd = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rd);
connn.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
GridView1.DataSource = dt;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow Grow in GridView1.Rows)
{
CheckBox chselect = (CheckBox)Grow.FindControl("chkSelect");
Label lblDEPID = (Label)Grow.FindControl("lblDEPID");
Label labelDEPNAME = (Label)Grow.FindControl("lblDEPNAME");
Label labelMANAGERNAME = (Label)Grow.FindControl("lblMANAGERNAME");
TextBox textDEPNAME = (TextBox)Grow.FindControl("txtDEPNAME");
TextBox textMANAGERNAME = (TextBox)Grow.FindControl("txtMANAGERNAME");
labelDEPNAME.Visible = true;
labelMANAGERNAME.Visible = true;
textDEPNAME.Visible = false;
textMANAGERNAME.Visible = false;
if (chselect.Checked)
{
labelDEPNAME.Visible = false;
labelMANAGERNAME.Visible = false;
textDEPNAME.Visible = true;
textMANAGERNAME.Visible = true;
OleDbConnection connn = new OleDbConnection(@"Provider=Microsoft.ACE.OleDb.12.0;Data Source=C:\Users\sivaranjani\Documents\Database6.accdb");
connn.Open();
OleDbCommand cmd = new OleDbCommand("update tab set DEPNAME='" + textDEPNAME.Text + "',MANAGERNAME='" + textMANAGERNAME.Text + "'where DEPID='" + Convert.ToInt32(lblDEPID.Text) + "'", connn);
cmd.ExecuteReader();
GridView1.EditIndex = -1;
ShowData();
}
}
ShowData();
}
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow Grow in GridView1.Rows)
{
CheckBox chselect = (CheckBox)Grow.FindControl("chkSelect");
Label lblDEPID = (Label)Grow.FindControl("lblDEPID");
Label labelDEPNAME = (Label)Grow.FindControl("lblDEPNAME");
Label labelMANAGERNAME = (Label)Grow.FindControl("lblMANAGERNAME");
TextBox textDEPNAME = (TextBox)Grow.FindControl("txtDEPNAME");
TextBox textMANAGERNAME = (TextBox)Grow.FindControl("txtMANAGERNAME");
labelDEPNAME.Visible = true;
labelMANAGERNAME.Visible = true;
textDEPNAME.Visible = false;
textMANAGERNAME.Visible = false;
if (chselect.Checked)
{
labelDEPNAME.Visible = false;
labelMANAGERNAME.Visible = false;
textDEPNAME.Visible = true;
textMANAGERNAME.Visible = true;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow Grow in GridView1.Rows)
{
CheckBox chselect = (CheckBox)Grow.FindControl("chkSelect");
Label lblDEPID = (Label)Grow.FindControl("lblDEPID");
if (chselect.Checked)
{
// Check Department exists in Employee table or not.
if (!this.IsDeptExist(Convert.ToInt32(lblDEPID.Text)))
{
OleDbConnection connn = new OleDbConnection(@"Provider=Microsoft.ACE.OleDb.12.0;Data Source=C:\Users\sivaranjani\Documents\Database6.accdb");
connn.Open();
OleDbCommand cmd = new OleDbCommand("delete from tab where DEPID='" + Convert.ToInt32(lblDEPID.Text) + "'", connn);
cmd.ExecuteReader();
Response.Write("<script>alert('Selected Row Deleted')</script>");
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('department is having employees so you cant delete the department')", true);
}
}
}
ShowData();
}
protected bool IsDeptExist(int depid)
{
OleDbConnection connn = new OleDbConnection(@"Provider=Microsoft.ACE.OleDb.12.0;Data Source=C:\Users\sivaranjani\Documents\Database6.accdb");
connn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * from emp where [DEPID]=@DID", connn);
cmd.Parameters.AddWithValue("@DID", depid);
OleDbDataReader rd = cmd.ExecuteReader();
DataTable tb = new DataTable();
tb.Load(rd);
if (tb.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
ShowData();
}