ASP.NET
  Home arrow ASP.NET arrow Page 5 - Creating a StudentDB Class for ASP.NET 2.0
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Creating a StudentDB Class for ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2007-09-05

    Table of Contents:
  • Creating a StudentDB Class for ASP.NET 2.0
  • Creating the Website and the App_Code Folder
  • Writing the Student class
  • Storing the Connection String in a Web.Config file
  • The StudentDB class

  • 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


    Creating a StudentDB Class for ASP.NET 2.0 - The StudentDB class


    (Page 5 of 5 )

    The studentDB class contains the methods InsertStudent() and UpdateStudent() which accept a parameter of type Student, and the method DeleteStudent() which accepts an integer value that represents the Student ID. Both InsertStudent() and UpdateStudent() methods do the insertion and the updating on the Students table by using the Student instance passed as a parameter to them.

    In other words, the Student instance passed to those methods represents a record in the Students table, and by calling a method like UpdateStudent() and passing it that Student instance, the database record (that this Student instance represents) will be updated with the values of the properties of that Student instance. The code that we write in this small series will crystallize this concept for you -- and after this series I will teach you another technique to perform the same operations on the Students table using other classes.

    The StudentDB class also contains the GetAllStudents() and GetStudent() methods which we will develop and discuss. Note that all these methods are static and we don't need to create instances to use them; it makes sense if you think about it. The only part that would be an instance is the object passed to the method as a parameter. You create an instance of the Student class and pass it to a method like InsertStudent() which will do its job and insert a record that represents that Student object into the database. Now let's look at this class:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    // additional namespaces needed for this class
    using System.Data.SqlClient;
    using System.Web.Configuration;
    using System.Collections.Generic;

    public class StudentDB
    {
      private readonly static string connString;

      static StudentDB()
      {
        connString = WebConfigurationManager.ConnectionStrings
    ["SchoolConnectionString"].ConnectionString;
      }

      public static int InsertStudent(Student student)
      { }

      public static void UpdateStudent(Student student)
      { }

      public static void DeleteStudent(int studentId)
      { }

      public static Student GetStudent(int studentId)
      { }

      public static DataTable GetAllStudentsInDataTable()
      { }

      public static List<Student> GetAllStudentsInCollection()
      { }
    }

    These are the methods we are going to develop in the series. For now just keep in mind that all the members are static and the connString is a private readonly string member that is assigned its value using the static constructor with the syntax we have discussed in the previous section. Also note that we have added three additional namespace references. The first is a namespace reference to the ADO.NET SQL Server Data Provider and the second is needed by the constructor's code because the WebConfigurationManager class lives in the namespace System.Web.Configuration. The third namespace is needed because we are using C# Generics in the GetAllStudentsInCollection() method.

    C# Generics is a great new feature that has been added in .NET Framework 2.0 which gives you the ability to create a strongly typed collection without writing any code. we simply use the .NET-provided generic collection types, like List<>, and tell it what type we need to use to create a strongly typed collection. In our case, we need a collection of Student objects, so we have used the syntax List<Student>. As you will see, you don't have to create a collection class and use an inner collection object, like the ArrayList, along with public methods, like Add(Student student), that accept the strongly typed object with which you want to work. All what you have to do is use generics.

    In the next article, we are going develop the GetStudent() and GetAllStudentsInCollection() methods.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · i haven't developed any real projects before because i still learning C# and asp.net...
       · Hi, Of course, you can read the article. It's not that difficult if you have...
       · A very good explanation, I visited ASP.net for first time and i find the very first...
       · Thanks, the next 2 article will be interesting more than this one. You will...
       · Very good article and can't wait for the second and third parts. are you going to...
       · Very good article and can't wait for the second and third parts. are you going to...
       · You will like the following articles even more.About the SqlDataSource control, I...
       · Nice article Michael but I need the other articles to complete the class. when we...
       · You will be reading the articles in the next few weeks. Tell me what do you think...
       · If you have an idea about an article related to ASP.NET 2.0 Data Access, please send...
       · Don't get me wrong the article is interesting. However, since it is broken in...
     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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