ASP.NET
  Home arrow ASP.NET arrow Page 2 - Developing Methods for the StudentDB Class...
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

Developing Methods for the StudentDB Class for ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-09-11

    Table of Contents:
  • Developing Methods for the StudentDB Class for ASP.NET 2.0
  • Creating the INSERT, UPDATE and DELETE methods
  • Explaining the code for the methods
  • Testing the Data Access Methods
  • The complete code for the Student and StudentDB Classes

  • 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


    Developing Methods for the StudentDB Class for ASP.NET 2.0 - Creating the INSERT, UPDATE and DELETE methods


    (Page 2 of 5 )

    Next we'll create the code for the three methods, InsertStudent(), UpdateStudent() and DeleteStudent(), that call the stored procedures created in the previous section which provide us with INSERT, UPDATE and DELETE operations on the Students table. Add the following methods to the StudentDB class.

    public static int InsertStudent(Student student){
      try{
        using (SqlConnection connection = new SqlConnection
    (connString)){
          SqlCommand command = new SqlCommand("InsertStudent",
    connection);
          command.CommandType = CommandType.StoredProcedure;
          command.Parameters.Add("@FirstName", SqlDbType.NVarChar,
    20).Value = student.FirstName;
          command.Parameters.Add("@LastName", SqlDbType.NVarChar,
    20).Value = student.LastName;
          command.Parameters.Add("@DateOfBirth",
    SqlDbType.DateTime).Value = student.DateOfBirth;
          command.Parameters.Add("@AdmissionDate",
    SqlDbType.DateTime).Value = student.AdmissionDate;
          command.Parameters.Add("@Major", SqlDbType.NVarChar,
    40).Value = student.Major;
          command.Parameters.Add("@Active", SqlDbType.Bit).Value =
    student.Active;
          command.Parameters.Add("@StudentID", SqlDbType.Int);
    command.Parameters["@StudentID"].Direction =
    ParameterDirection.Output;

          connection.Open();
          command.ExecuteNonQuery();
          return (int) command.Parameters["@StudentID"].Value;
        }
      }
      catch (SqlException ex){
        throw new ApplicationException("A database error
    has occurred.");
      }
      catch (Exception ex){
        throw new ApplicationException("An error has occurred.");
      }
    }

    public static void UpdateStudent(Student student){
      try{
        using (SqlConnection connection = new SqlConnection
    (connString)){
          SqlCommand command = new SqlCommand("UpdateStudent",
    connection);
          command.CommandType = CommandType.StoredProcedure;
          command.Parameters.Add("@StudentID", SqlDbType.Int).Value =
    student.StudentId;
          command.Parameters.Add("@FirstName", SqlDbType.NVarChar,
    20).Value = student.FirstName;
          command.Parameters.Add("@LastName", SqlDbType.NVarChar,
    20).Value = student.LastName;
          command.Parameters.Add("@DateOfBirth",
    SqlDbType.DateTime).Value = student.DateOfBirth;
          command.Parameters.Add("@AdmissionDate",
    SqlDbType.DateTime).Value = student.AdmissionDate;
          command.Parameters.Add("@Major", SqlDbType.NVarChar,
    40).Value = student.Major;
          command.Parameters.Add("@Active", SqlDbType.Bit).Value =
    student.Active;

          connection.Open();
          command.ExecuteNonQuery();
        }
      }
      catch (SqlException ex){
        throw new ApplicationException("A database error has
    occurred.");
      }
      catch (Exception ex){
        throw new ApplicationException("An error has occurred.");
      }
    }

    public static int DeleteStudent(int studentId){
      try{
        using (SqlConnection connection = new SqlConnection
    (connString)){
          SqlCommand command = new SqlCommand("DeleteStudent",
    connection);
          command.CommandType = CommandType.StoredProcedure;
          command.Parameters.Add("@StudentID", SqlDbType.Int).Value =
    studentId;
          connection.Open();
          return command.ExecuteNonQuery();
        }
      }
      catch (SqlException ex){
        throw new ApplicationException("A database error has
    occurred.");
      }
      catch (Exception ex){
        throw new ApplicationException("An error has occurred.");
      }
    }

    Let's talk about those methods.

    More ASP.NET Articles
    More By Michael Youssef


       · Today we complete the StudentDB class, you need to read the previous 2 articles to ...
       · i used your code in my application and it's nice. thank you very much for the...
       · It would be better to use the code with the ObjectDataSource control and using a...
     

    ASP.NET ARTICLES

    - 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
    - Building a Simple Storefront with LINQ





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