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

preview_player
Показать описание

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);
}
Рекомендации по теме
Комментарии
Автор

Venkat: I may repeat myself, but I can't thank you enough for these videos. I just may get a job because of you and your help. Time and time again, I find your resources the authority on how to do things.
Now after all that, I'd love to see you explain three tiered architecture with separate DALs and BLLs. Regardless, thanks so much for your help.

MrHooglaz
Автор

Thank you so much. This saved my life. I actually followed step by step for a project I am doing at work. 

myspanishtaste
Автор

Sure, I will record the concepts you mentioned as soon as I can.

Csharp-video-tutorialsBlogspot
Автор

Hi sir,
your videos are very useful to me, the way you explaining is awesome sir ...You covered lot of things in .Net.
Can you please upload a video regarding what are the standards need to be follow while we coding for project, like creating entity classes with properties and layer programming, 3-tier and n-tier architecture.
Am very glad if do that...Thank you for your contribution in Spreading knowledge to the world

rajmahendervarma
Автор

in previous video, rowcommand is used. Why use SelectedIndexChanged now? i'm curious

jumbo
Автор

I think  we can get the reference to the ID by using detailviews datakeynames  property instead of getting the reference  of ID from gridview  control  using selectedDatakey.value property when we are updating the detailview. Its just a thought.

andyb
Автор

Sir I need help for how to grid view template field button click work for update data from SQL table... click on grid view column button how work this button can you give me one example how to update data from grid view button click

ashwiniingole
Автор

Sir When i done this same ur application i got 1 Exception "Null Object Ref. Exception " On Every Inserting, Updating and Deleting Event and even i set DataKeyNames = eid with Details View Control .i m unable to fix it Plz gv me feedback sir for this prbolem solution...

PrinceAgrawal
Автор

Suppose we are storing Gender Id Male as 1 , Female as 2 then how to update the Gender

shahzaibriaz
Автор

Not sure why mine isn't working, when I press the edit link the mode changes but the textboxes are empty.

BigyetiTechnologies