Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 5 - Using Methods with 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

Using Methods with Object Oriented Database Development with VB.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 16
    2006-07-19

    Table of Contents:
  • Using Methods with Object Oriented Database Development with VB.NET 2005
  • Developing methods with parameters having default values
  • Dealing with method overloading to attain more flexibility
  • Developing methods with parameters as objects
  • Developing methods which return objects

  • 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

    Using Methods with Object Oriented Database Development with VB.NET 2005 - Developing methods which return objects


    (Page 5 of 5 )

    In the previous section, we developed methods accepting parameters as objects.  In this section, we shall examine the methods returning objects.  Let us look at the code first:

    PublicFunction getEmployee(ByVal empno As String) As Emp
            Dim objEmp As New Emp
            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
                objEmp.m_empno = empno
                cmd.Connection.Open()
                Dim rd As SqlDataReader = cmd.ExecuteReader
                If rd.Read() Then
                    objEmp.m_ename = rd("ename")
                    objEmp.m_sal = rd("sal")
                    objEmp.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

            Return objEmp
        End Function

    Within the above code, you can observe the object named "objEmp." I created it just to place values in it and return the same. Through SqlDataReader, I retrieved all the values and assigned them to the fields of "objEmp."  Finally, I returned the same.

    To execute the above code, I wrote the following code:

    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
                Dim objReturned As Emp
                objReturned = ep.getEmployee(Me.txtEmpno.Text)
                Me.txtDeptno.Text = objReturned.m_deptno
                Me.txtEname.Text = objReturned.m_ename
                Me.txtSal.Text = objReturned.m_sal
            Catch ex As Exception
                Me.lblErrMsg.Text = ex.Message
            End Try
        End Sub

    The most important issue is the object named "objReturned."  It is not a complete object (as it is not declared with a "new" operator).  It is simply an "object Reference." That means it refers to some other object assigned to it (but it doesn't allocate any memory by itself). In the current case, "getEmployee" creates memory allocation and refers it with "objEmp." Later the same is assigned to "objReturned" (which "refers" to the memory allocated by some other object).

    In this article, I simply wanted to explain the topics related to OOPS along with data access. The sample codes given in this article are neither the best in performance nor the best in programming methodologies.  My upcoming articles will deal with these issues.

    Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.


    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.

       · Hello guys! this is my next contribution on database oriented OOPS with VB.NET 2005....
     

    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 2 hosted by Hostway