ASP.NET GridView Tutorial
Lesson 204 of 301 · asp.net detailsview insert update delete without using data source controls – Part 42 · Database
asp.net detailsview insert update delete without using data source controls – Part 42
Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
Link for text version of this video
http://csharp-video-tutorials.blogspot.com/2013/04/aspnet-detailsview-insert-update-delete.html
In this video we will discuss about inserting, updating and deleting data from detailsview without using any datasource control.
When the webform is loaded, the gridview control, should retrieve and display all the rows from tblEmployee table. As soon as I select a row, in the gridview control, then all the columns of the selected row, should be displayed in details view control. Then, we should be able to edit, delete and add a new employee using the DetailsView control.
We will be using tblEmployee table for this demo. If you need sql script to create this table, please refer to Part 37 using the link below.
http://csharp-video-tutorials.blogspot.com/2013/03/detailsview-in-aspnet-part-37.html
We implemented EmployeeDataAccessLayer with methods to Select, Insert, Update and Delete data in Part 41. We will be using EmployeeDataAccessLayer for this demo, if you need the code, please refer to Part 41 using the link below.
http://csharp-video-tutorials.blogspot.com/2013/03/aspnet-detailsview-insert-update-delete_31.html
Step 1: Drag and drop “GridView” control on WebForm1.aspx
Step 2: On GridView1, set
DataKeyNames=Id
AutoGenerateSelectButton=”true”
Step 3: Generate event handler method for SelectedIndexChanged event of GridView1 control
Step 4: Drag and drop “DetailsView” control on WebForm1.aspx
Step 5: On DetailsView1, set
DataKeyNames=Id
AutoGenerateRows=”False”
AutoGenerateSelectButton=”True”
AutoGenerateDeleteButton=”True”
AutoGenerateEditButton=”True”
AutoGenerateInsertButton=”True”
Setp 6: Add 10 BoundField’s and set DataField and HeaderText accordingly.
Setp 7: Set ReadOnly=”True” and InsertVisible=”False” on the bound field that display, Id column value.
Step 8: Generate event handler method for the following events of DetailsView1 control.
ModeChanging
ItemInserting
ItemDeleting
ItemUpdating
Step 9: Copy and paste the following code in webform1.aspx.cs file
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewDataBind();
}
}
private void GridViewDataBind()
{
GridView1.DataSource = EmployeeDataAccessLayer.GetAllEmployeesBasicDetails();
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DetailsViewDataBind();
}
private void DetailsViewDataBind()
{
if (GridView1.SelectedDataKey != null)
{
DetailsView1.Visible = true;
// In the line below change square bracket to angular bracket
List[Employee] listEmployee = new List[Employee]();
listEmployee.Add(EmployeeDataAccessLayer.GetEmployeesFullDetailsById((int)GridView1.SelectedDataKey.Value));
DetailsView1.DataSource = listEmployee;
DetailsView1.DataBind();
}
else
{
DetailsView1.Visible = false;
}
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
DetailsViewDataBind();
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
EmployeeDataAccessLayer.InsertEmployee(e.Values[“FirstName”].ToString(),
e.Values[“LastName”].ToString(), e.Values[“City”].ToString(),
e.Values[“Gender”].ToString(), Convert.ToDateTime(e.Values[“DateOfBirth”]),
e.Values[“Country”].ToString(), Convert.ToInt32(e.Values[“Salary”]),
Convert.ToDateTime(e.Values[“DateOfJoining”]), e.Values[“MaritalStatus”].ToString());
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
GridViewDataBind();
GridView1.SelectRow(-1);
}
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
EmployeeDataAccessLayer.DeleteEmployee((int)GridView1.SelectedDataKey.Value);
GridViewDataBind();
GridView1.SelectRow(-1);
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int employeeId = (int)GridView1.SelectedDataKey.Value;
EmployeeDataAccessLayer.UpdateEmployee(employeeId,
e.NewValues[“FirstName”].ToString(), e.NewValues[“LastName”].ToString(),
e.NewValues[“City”].ToString(), e.NewValues[“Gender”].ToString(),
Convert.ToDateTime(e.NewValues[“DateOfBirth”]), e.NewValues[“Country”].ToString(),
Convert.ToInt32(e.NewValues[“Salary”]), Convert.ToDateTime(e.NewValues[“DateOfJoining”]),
e.NewValues[“MaritalStatus”].ToString());
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
GridViewDataBind();
GridView1.SelectRow(-1);
}