ASP.NET
  Home arrow ASP.NET arrow Adding Methods to the StudentDataAccess Cl...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Adding Methods to the StudentDataAccess Class for ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-09-18

    Table of Contents:
  • Adding Methods to the StudentDataAccess Class for ASP.NET 2.0
  • The InsertNewStudent() and UpdateStudent() methods
  • Testing the methods from a Web page
  • Creating the DeleteStudent() method
  • The StudentDataAccess class, the complete code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Adding Methods to the StudentDataAccess Class for ASP.NET 2.0


    (Page 1 of 5 )

    Today we continue developing the StudentDataAccess class we discussed in the previous two articles. In particular, we will create the InsertNewStudent(), UpdateStudent(), DeleteStudent() and finally the SaveStudent() methods. Keep reading to learn more in this conclusion to a three-part series.

    Both the InsertNewStudent() and the UpdateStudent() will be defined as private whereas the DeleteStudent() and the SaveStudent() methods will be defined as public. When the client application, the web page in our case, calls the SaveStudent() method it will determine whether it needs to insert a new student record in the database or update the existing one based on the studentId field's value of the StudentDataAccess class. As we have seen in the previous article, we have used the studentId field's value to make a few decisions, and we continue using it today.

    For example, in the parameterized constructor of the StudentDataAccess class, the one that accepts the studentId value, we built our code to test whether the user has supplied a valid studentId value; if we didn't find a matching record in the database we set the studentId field's value to -1, which means that we couldn't initialize the object from the database.

    Today, we use the studentId field to determine if the user is inserting a new record or updating an existing one. Add the following SaveStudent() method definition to the StudentDataAccess class:

    public void SaveStudent()
    {
    if (this.studentId == -1)
    this.InsertNewStudent();
    else
    this.UpdateStudent();
    }

    Note that the SaveStudent() method is defined as public, and it's not a static method, because it works on instances of the StudentDataAccess class, which makes sense. In this method we use the studentId field's value to determine what we want to do. If the value is equal to -1, which means that the current StudentDataAccess instance has not been initialized from the database, then we call the instance private method InsertNewStudent() to insert a new record in the Students table.

    If the value of the studentId field is not equal to -1 then we know that we have initialized the StudentDataAccess instance from the database. This is correct only if the user has assigned values to the properties of the current StudentDataAccess object, and the caller called the SaveStudent() method to submit changes made to the current StudentDataAccess instance to the database, and we need to call the UpdateStudent() method.

    Let's discuss both the InsertNewStudent() and UpdateStudent() methods that are conditionally called from the SaveStudent() method.

    More ASP.NET Articles
    More By Michael Youssef


       · Ok by now we have a complete data access class. It's also good to read the 3...
     

    ASP.NET ARTICLES

    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT