Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - An Introduction to Object Oriented Databas...
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 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
VISUAL BASIC.NET

An Introduction to Object Oriented Database Development with VB.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2006-07-12

    Table of Contents:
  • An Introduction to Object Oriented Database Development with VB.NET 2005
  • Explaining the class
  • Explanation continued
  • Expanding the class to handle errors
  • Further expanding the class to handle additional rows

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    An Introduction to Object Oriented Database Development with VB.NET 2005 - Expanding the class to handle errors


    (Page 4 of 5 )

    Any application must include error handling. If you do not include error handling, the application may get terminated abnormally when a run-time error occurs.  Run-time errors are a bit hard to detect and identify; it requires some experience.  Some types of run-time errors include database connectivity errors, row/table not found errors, network errors, data type conversion errors, and so forth.

    The above class (and application) currently does not include any errors. To handle the errors, we need to work with “exception handling.”  We trap run-time errors using exception handling and provide our own alternative to run our application smoothly. We do this by giving proper, understandable messages to the user,  without any abnormal termination.

    The method “load” in the class “emp” can be rewritten as follows to include exception handling:

        Public Sub load(ByVal empno As String)
            Dim cn As New SqlConnection("Data Source=.sql2k5;initial catalog=sample;user id=sa;password=eXpress2005")
            Dim cmd As New SqlCommand("select * from sample.dbo.emp where empno='" & empno & "'", cn)
            Try
                m_empno = empno
                cmd.Connection.Open()
                Dim rd As SqlDataReader = cmd.ExecuteReader
                If rd.Read() Then
                    m_ename = rd("ename")
                    m_sal = rd("sal")
                    m_deptno = rd("deptno")
                Else
                    Throw New Exception("Employee not found")
                End If
            Catch ex As Exception
                Throw New Exception(ex.Message)
            Finally
                If cmd.Connection.State = ConnectionState.Open Then
                    cmd.Connection.Close()
                End If
                cmd.Dispose()
                cn.Dispose()
            End Try

        End Sub

    If any error occurs in the above method "load," it will be thrown back to the calling program (in this case, it is the application itself).  So, we need to handle the exceptions thrown to the main application as follows:

    PrivateSub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
            Me.lblErrMsg.Text = ""
            Try
                Dim ep As New Emp
                ep.load(Me.txtEmpno.Text)
                Me.txtDeptno.Text = ep.m_deptno
                Me.txtEname.Text = ep.m_ename
                Me.txtSal.Text = ep.m_sal
            Catch ex As Exception
                Me.lblErrMsg.Text = ex.Message
            End Try
        End Sub

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys. This series introduces you the OOPS in VB 2005. enjoy
       · I am unable to download the zip file.
       · The link has been corrected. Thank you for pointing out the link and we apologize...
       · Hello jagdish. i wanna to ask you, being a newbie in vb.net 2005, is this the best...
       · Hai,This series explains you the simplest way to develop OOP based db...
     

    VISUAL BASIC.NET ARTICLES

    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic





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