Building an ASP.NET Phonebook Web Application - Part 2
In Part 1 we have set the foundations. Now we have to build the application. In Part 2 we''ll do everything that's related with Persons (insert/edit/delete person and see persons phone numbers).Â
Part 2: Building an application
Create folders MODEL and DAL. Model folder contains entities - classPerson and class PhoneNumber, that have the properties that correspond to the database Persons and PhoneNumbers table columns. DAL folder has also two classes - PersonDAL, PhoneNumberDAL, and in each class corresponding methods.
In order to make our page create new aspx file - e.g. Phonebook.aspx or Default.aspx. Pages are written using a combination of HTML, server controls, and server code (what is web forms?).
First, we need to know in which way we'll display data (e.g. table, list etc.). Our list of persons should be presented in a table. We can use several controls to do that but for this case I used Repeater (it's simple). Add "Insert New Person" button after the Repeater.
Also, go to code-behind file - Phonebook.aspx.cs and in Page_Load event we need to add data source and bind data to repeater.
Save, build and view in browser. It should look something like this:
Now we need to add page for the list of numbers for particular person and page for editing that person. Also, add event to Insert button and Delete LinkButton and page for inserting new person.
1. PhoneNumber.cs &Â GetNumbersByPersonID Method
We need to have the following two classes and method in order to continue.
Note! List of phone numbers is for that particular person. So our method for getting those numbers needs to receive that persons ID and you can find that ID in query string.
GetNumbersByPersonID Method:
2. Create PhoneNumbers.aspx file
This time I'll use GridView for showing data. Manage columns, add data source and bind data. In Phonebook_aspx update Phone href:Â <a href='PhoneNumbers.aspx?id=<%# Eval("PersonID") %>'>Phone</a>.
Save, Build, Test! Now when you click on the Phone link we see numbers of those person.
When you edit a person you already have that person data, so we need to see current data in order to change them. In PersonDAL.cs add a method that receives person ID and returns persons first and last name.
We have the data, now we would like to change it. In PersonDAL.cs add the method for that updates.
Now we need page. Create EditPerson.aspx.
First we need to read current data and than when we change them we can update.
In Phonebook.aspx update Edit href: <a href='EditPerson.aspx?id=<%# Eval("PersonID") %>'>Edit</a>Â
In PersonDAL.cs add InsertPerson method.
Create InsertPerson.aspx:
In Phonebook_aspx below Repeater add button. Change ID, text and add OnClick event.
<asp:Button ID="btn_InsertNewPerson" runat="server" Text="Insert New Person" OnClick="btn_InsertNewPerson_Click" />
 Build and try to add new person! It should work.
In PersonDAL.cs add DeletePerson method.
Go to Phonebook.aspx to Delete column and replace
<td><a href="#">Delete</a></td> with
<td><asp:LinkButton ID="lbtn_Delete" runat="server" OnCommand="lbtn_Delete_Command" CommandArgument='<%# Eval("PersonID") %>'>Delete</asp:LinkButton></td>
When we delete a row we need to reload page. We don't wont to have double code so will'll extract data source and data bind code to FillPersonList method. Use that code in Page_Load and lbtn_Delete_Command:
Upadate Phonebook.aspx.cs
Save, build, test - doesn't work!
We have problem because we need to enable cascade delete so that when we delete person all phone numbers are deleted too.
Go to Management Studio and change Delete and Update Rule to Cascade. Now it should work.
Go to Part 3 and finish application.