03 Payroll ASP.NET C# SQL Server Bootstrap CRUD Operations in GridView Using ASP.Net
How to Edit ,Delete and Update Employee Record using GridView CRUD Operation.
Payroll Management System Project Part-3:
Step 1: Add a Gridview in AddEmployee.aspx Webpage
<asp:GridView ID="GridView1" CssClass="table table-sm table-condensed table-hover"
AutoGenerateColumns="false"
runat="server"
EmptyDataText="Record
Not Found..." DataKeyNames="ID" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound"
AllowPaging="True"
PageSize="50"
PagerSettings-Mode="Numeric" PagerSettings-NextPageText=">"
Font-Size="7pt">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"
Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblGV1Name"
runat="server"
Text='<%# Eval("Name")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGV1EditFullName"
runat="server"
Text='<%# Eval("Name")%>' Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblGV1DesiName"
runat="server"
Text='<%# Eval("DesignationName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditGV1DesignID"
runat="server"
Text='<%# Eval("DesignationID")%>'
Visible="false"></asp:Label>
<asp:DropDownList ID="ddlGV1Designation" runat="server"> </asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblGV1UserName"
runat="server"
Text='<%# Eval("UserName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGV1EditUserName"
runat="server"
Text='<%# Eval("UserName")%>'
Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblGV1Password"
runat="server"
Text='<%# Eval("Password")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGV1EditPassword"
runat="server"
Text='<%# Eval("Password")%>'
Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fathername">
<ItemTemplate>
<asp:Label ID="lblGV1Fathername"
runat="server"
Text='<%# Eval("Fathername")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditFathername"
runat="server"
Text='<%# Eval("Fathername")%>'
Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB">
<ItemTemplate>
<asp:Label ID="lblGV1DOB"
runat="server"
Text='<%# Eval("DOB")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGV1EditDOB"
runat="server"
Text='<%# Eval("DOB")%>' Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblGV1Address"
runat="server"
Text='<%# Eval("Address")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditAddress"
runat="server"
Text='<%# Eval("Address")%>' Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:Label ID="lblGV1Mobile"
runat="server"
Text='<%# Eval("MobileNo")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditMobile"
runat="server"
Text='<%# Eval("MobileNo")%>' Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblGV1Email" runat="server" Text='<%# Eval("Email")%>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditEmail"
runat="server"
Text='<%# Eval("Email")%>' Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BasicSalary">
<ItemTemplate>
<asp:Label ID="lblGV1BasicSalary"
runat="server"
Text='<%# Eval("BasicSalary")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGV1EditBasicSalary"
runat="server"
Text='<%# Eval("BasicSalary")%>'
Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOJ">
<ItemTemplate>
<asp:Label ID="lblGV1DOJ"
runat="server"
Text='<%# Eval("DOJ")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGV1EditDOJ"
runat="server"
Text='<%# Eval("DOJ")%>' Style="text-transform:
uppercase"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"
ControlStyle-CssClass="btn
btn-sm btn-primary" />
<asp:CommandField ShowDeleteButton="true"
ButtonType="Button"
ControlStyle-CssClass="btn
btn-danger btn-sm" />
</Columns>
</asp:GridView>
Step #2: Write Following C# Code in AddEmployee.aspx.cs page
private void BindGrid()
{
SqlConnection con = new
SqlConnection(GetConnectionString());
SqlCommand cmd = new
SqlCommand("select
t1.Id,t1.Name,t1.Designation as DesignationID ,t2.Designation as
DesignationName,
t1.Username,t1.Password,t1.Fathername,t1.DOB,t1.Address,t1.MobileNo,t1.Email,t1.BasicSalary,t1.DOJ
from addemployee as t1 inner join tblDesignation as t2 on t2.ID=t1.Designation",
con);
SqlDataAdapter da = new
SqlDataAdapter(cmd); DataTable
dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
} else {
GridView1.DataSource = null; GridView1.DataBind();
}
}
protected void
GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
protected void
GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void
GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void
GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//--Delete Selected Row
Label id = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblID"));
int id2 = Convert.ToInt32(id.Text);
SqlConnection con = new
SqlConnection(GetConnectionString());
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string cmdstr = "DELETE
FROM addemployee WHERE ID=@ID;";
SqlCommand cmd = new
SqlCommand(cmdstr, con);
cmd.Parameters.AddWithValue("@ID",
id2);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindGrid();
}
protected void
GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label idq = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblID"));
int id2 = Convert.ToInt32(idq.Text);
TextBox txtEditFullNameUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGV1EditFullName");
DropDownList ddlGV1DesignationUpdate = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlGV1Designation");
TextBox txtEditUsernameUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGV1EditUserName");
TextBox txtEditPasswordUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGV1EditPassword");
TextBox txtEditFatherNameUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditFathername");
TextBox txtEditDOBUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGV1EditDOB");
TextBox txtEditAddressUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditAddress");
TextBox txtEditmobileUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditMobile");
TextBox txtEditEmailUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditEmail");
TextBox txtEditSalaryUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGV1EditBasicSalary");
TextBox txtEditDOJUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGV1EditDOJ");
SqlConnection con = new
SqlConnection(GetConnectionString());
SqlCommand cmd = new
SqlCommand("update
addemployee set
Name=@Name,Designation=@Designation,Username=@Username,Password=@Password,Fathername=@Fathername,DOB=@DOB,Address=@Address,MobileNo=@MobileNo,Email=@Email,BasicSalary=@BasicSalary,DOJ=@DOJ
where ID=@ID", con);
cmd.CommandType
= CommandType.Text;
cmd.Connection = con;
if (con.State == ConnectionState.Closed)
{
cmd.Connection.Open();
}
cmd.Parameters.AddWithValue("@Name",txtEditFullNameUpdate.Text
);
cmd.Parameters.AddWithValue("@Designation",ddlGV1DesignationUpdate.SelectedValue
);
cmd.Parameters.AddWithValue("@Username",
txtEditUsernameUpdate.Text);
cmd.Parameters.AddWithValue("@Password",txtEditPasswordUpdate.Text
);
cmd.Parameters.AddWithValue("@Fathername",txtEditFatherNameUpdate.Text
);
cmd.Parameters.AddWithValue("@DOB",txtEditDOBUpdate.Text
);
cmd.Parameters.AddWithValue("@Address",txtEditAddressUpdate.Text
);
cmd.Parameters.AddWithValue("@MobileNo",txtEditmobileUpdate.Text
);
cmd.Parameters.AddWithValue("@Email",txtEditEmailUpdate.Text
);
cmd.Parameters.AddWithValue("@BasicSalary",
Convert.ToDouble(txtEditSalaryUpdate.Text));
cmd.Parameters.AddWithValue("@DOJ",
txtEditDOJUpdate.Text);
cmd.Parameters.AddWithValue("@ID",
id2);
cmd.ExecuteNonQuery();
cmd.Connection.Close();
GridView1.EditIndex = -1;
BindGrid();
}
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow
&& GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddlEditBindDesignation = (DropDownList)e.Row.FindControl("ddlGV1Designation");
//------------------------Bind
Designation ----------------
SqlConnection con1 = new
SqlConnection(GetConnectionString());
SqlCommand cmd1 = new
SqlCommand("select
distinct ID,Designation from tblDesignation where Designation!=''",
con1);
if (con1.State == ConnectionState.Closed)
{
con1.Open();
}
cmd1.CommandType = CommandType.Text;
SqlDataAdapter da1 = new
SqlDataAdapter(cmd1);
DataTable dt1 = new
DataTable();
da1.Fill(dt1);
con1.Close();
if (dt1.Rows.Count > 0)
{
ddlEditBindDesignation.DataSource = dt1;
ddlEditBindDesignation.DataValueField = "ID";
ddlEditBindDesignation.DataTextField = "Designation";
ddlEditBindDesignation.DataBind();
ListItem ddlw3 = new ListItem("Select Option", "-1");
ddlEditBindDesignation.Items.Insert(0, ddlw3);
Label lblDesigID = (Label)e.Row.FindControl("lblEditGV1DesignID");
ddlEditBindDesignation.SelectedValue = lblDesigID.Text;
}
else
{
ddlEditBindDesignation.DataSource = null;
ddlEditBindDesignation.DataBind();
//ddlEditMT.Items.Clear();
ddlEditBindDesignation.SelectedIndex = -1;
}
//-------------------------End------------------
}
//----------Confirm Delete after delete button click
event---
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[0].Text;
foreach (Button
button in e.Row.Cells[13].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"]
= "if(!confirm('Do you want to delete " + e.Row.Cells[0].Text + "?')){ return false; };";
}
}
}
//-----------------End
}
Sir is ki database file kaha ha
ReplyDelete