filmov
tv
asp.net detailsview insert update delete without using data source controls - Part 42

Показать описание
Link for text version of this video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
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.
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.
Step 2: On GridView1, set
DataKeyNames=Id
AutoGenerateSelectButton="true"
Step 3: Generate event handler method for SelectedIndexChanged event of GridView1 control
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
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);
}
Комментарии