Previous Next
Programmers Guide for .NET Chapter 3 - Developing ASP.NET Application

Chapter 3 - Developing ASP.NET Application

With DB Visual ARCHITECT (DB-VA) you can develop quality ASP.NET Web Application much faster, better and cheaper. All DB-VA generated C# code, configuration files and persistent layer library are deployable to Internet Information Services (IIS). DB-VA generates all C# code for accessing database. You do not need to write SQL to insert, query, update or delete the record. All code you need to program is plain C# code (e.g. OrderDAO.Save(myOrder);). In this chapter we will use a simple "School System" application to show you how to generate C# code, configure your web application, and create/query/update/ delete objects. Again you do not need to write a single SQL statement for all above operations.

Figure 3.1 - The architenture of ASP .NET application with DB-VA Persistent Layer

In this chapter:

Introduction

You will develop a School System.

The School System provides the following functions:

Required Software:

Please open the SchoolSystem.vpp project file in the Chapter 3 School System.zip file. The project file contains the following diagrams. Please refer to the Designer Guide for details about how to draw class diagram and entity relationship diagram.

Class Diagram of School System:

Figure 3.2 - Class Diagram

Entity Relationship Diagram of School System:

Figure 3.3 - Entity Relationship Diagram (ERD)

Creating Object and Saving to Database

Before writing code to develop the ASP.NET application, you need to:

  1. Generate the C# code for accessing database by DB-VA
  2. Create the ASP.NET Web Application project.
  3. Add generated C# project to Solution which contain the ASP.NET Web Application project.
  4. Add the generated C# project and persistent library to the references of the ASP.NET Web Application project.

The details of how to setup the environment to develop the ASP.NET application, please refer to Chapter 2 - Configuring the Source and Library in Visual Studio.

The school system provides separate register pages for teacher and student to enter their information. The register method of teacher and student method is the same, so we only demonstrate how to create the teacher and save to database.

  1. Create the RegisterUserComponent. It contains Login ID, User Name, Password and Email Field for User to input information. It has RequestedFieldValidators to check the Login ID and Password field so they do not allow blank. It can be reused in teacher and student register page.
  2. Figure 3.4 - The RegisterUserComponent
  3. Create a Web Form called teacherReg.aspx and drag the RegisterUserControl to it.
  4. Figure 3.5 - The Web Form
  5. Double click the Submit button to create the submitButton_Click method to handle the button click event.
  6. private void submitButton_Click(object sender, System.EventArgs e)
    {
    ...
    }

    Source Code : webschoolsystem\teacherReg.aspx.cs

  7. Use RegisterUserComponent information to create the Teacher in system.

Querying Object from Database

You can retrieve the record in database as object. For example, you need to create the login function for the School System. It will request the user to input the user ID and password to login, the system retrieve the User object with the user id and compare the password to validate the user.

  1. Create the Login.aspx. It is used for user to login to the school system.
  2. Figure 3.8 - Login page
  3. Redirect the user to the student page or the teacher page when user is login. Try to get the User object from http session.
  4. private void redirectLoginedUser()
    {
    Object lObj = Session.Contents["user"];
    if (lObj != null)
    {
    if (lObj is Teacher)
    {
    Response.Redirect("teacherPage.aspx", true);
    }
    else if (lObj is Student)
    {
    Response.Redirect("studentPage.aspx", true);
    }
    }
    }

    Source Code : webschoolsystem\login.aspx.cs

  5. Get the User object by the user input Login ID and compare the user input password and the object password in the Submit button click event. If the password matches then user can access the system.

Updating Object and Saving to Database

You can modify the teacher information and update the record in database. You get the User object from the session and set the new values for the User object, finally call save() method to update the record in database.

  1. Click Modify Personal Information hyperlink to edit the user information.
  2. Figure 3.9 - Teaching Page
  3. It shows user information and allows you to update the user information except Login ID.
  4. Figure 3.10 - To modify the user information
  5. Click the submit button and it will set the updated information to the User object from the Http Session.
  6. Object lObj = Session.Contents["user"];
    if (lObj == null)
    {
    Response.Redirect("login.aspx");
    }
    else
    {
    school.SchoolSystemPersistentManager.Instance().GetSession().Lock(lObj, NHibernate.LockMode.None);
    }
  7. Update information of User object.
  8. user.Name = registerUserControl.UserName;
    user.Password = registerUserControl.Password;
    if (user is Teacher)
    {
    ((Teacher)user).Email = registerUserControl.Email;
    }
    user.Save();
    t.Commit();

    Source Code : webschoolsystem\login.aspx.cs

    Figure 3.11 - The record in database are modified

Deleting Object in Database

Teachers can create course for students to enroll and they can cancel courses in the system. They only need to click cancel hyperlink of the course then the course information will be deleted in the database and all the relationship with registered students will be removed.

  1. Teacher can create course by selecting the Create Course hyperlink in teacher page. Fill in Course name and Description to create Course:
  2. Figure 3.12 - Delete record
  3. Student can register the course in the student page.
  4. Figure 3.13 - Student Page
  5. The teacher can view how many students have registered his course, and he can cancel the course.
  6. Figure 3.14 - Teacher Page
  7. Click Cancel of a Course then it will pass the course id to the function:

Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com