Database Code
  Home arrow Database Code arrow Page 5 - Using ADO to Communicate with the Database...
Click Here
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? 
DATABASE CODE

Using ADO to Communicate with the Database, Part 1
By: Pradeep Chaudhary
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 34
    2004-11-02

    Table of Contents:
  • Using ADO to Communicate with the Database, Part 1
  • What is a Recordset?
  • What Are the Lock Types?
  • Bringing All Together
  • Implementing the 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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Using ADO to Communicate with the Database, Part 1 - Implementing the Code


    (Page 5 of 5 )

    Step-III (Implementing the Code)

    1. Double Click the form Window, it will open the Editor Window of “frmMyAddrBook”

    2. Declare some Variables in the General Declaration Section of the Code.

    Dim objConn As ADODB.Connection ‘ Connection Object
    Dim objRs As ADODB.Recordset
    Dim strConnString As String ‘ Variable to Hold the Connection String
    Dim strQstr As String ‘ Variable to store Query
    Dim sSelID As Double
    Dim sOperateMode As Strign

    3. Write a procedure to clear the controls on the form

    Private Sub ClearControls()
    ‘ This procedure will clear the controls on the form.
    TxtName=””
    TxtAddr=””
    TxtTel=””
    End Sub

    4. Call this procedure from ther Clear Button Click Event

    Private Sub cmdClear_Click()
    ClearControls ‘ it can be called like Call ClearControls
    End Sub

    5. Establish Connection with The database

    Private Function bgetConnected()As Boolean
    On Error goto ConnErr
    GetConnectedb=False
    Set objConn = New ADODB.Connection
    StrConnStr= "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=” App.path & “mydeb.mdb"
    Objconn.Open StrConnStr
    bGetConnected=true
    Exit Function
    ConnErr:
    Msgbox “Unable to Connect to the Database becaue” & Err.Description, VbInformation
    End Function

    6. Loading the Already Existing data to the List

    7. Now Call This procedure in the Form_Load Event so that data will remain blank

    Private Sub Form_Load()
    Call ClearContriols ‘ Calling procedure to clear the input controls
    SOperateMode=””
    BgetConnected ‘ Establishing Connection with the Database
    End Sub

    8. Add the Code below to Add Button Click Event

    Private cmdAdd_Click()
    Call Clearcontrols ‘ Clear Data
    Operatemode=”Add” ‘ Seting Operation Mode
    End Sub

    9. Add a procedure to validate the Data, like Name and Address Must be entered before we save the Data.

    Private Function bValidateData() As Boolean
    BValidateData=False ‘ Default we r assuming that Data is Invalid
    If (Trim(txtName)=”” Then
    Msgbox”Please Enter Name”,Vbinformation,App.title
    Trim(txtName.Setfocous ‘Seting focus to Name filed
    Exit Function
    Endif
    ‘’’ Place similar Validation for Address here
    ‘ .... .... ...
    BValidateData=True
    End Function

    10. Now We need to Save the data, so we will be writing a single procedure which will save the Data (Add/Edit/Delete)

    Private bSaveData() As Boolean
    On Error goto ErrHandlr ‘ incorporating Error Handler
    BSaveData=false
    StrQstr=””
    If sOperateMode=”Add” Then
    StrQstr=”Insert Into tblAddrBook (Name, Addr,Tel) Values(“
    StrQstr=strQstr & “’” & txtName & “’,” & txtAddr & “’,’” & txtTel &”’’)”

    Else if sOperateMode=”Edit” Then
    StrQstr=”Update tblAddrBook Set”
    StrQstr=strQstr & “ Name=’” & txtName & “’,”
    StrQstr=strQstr & “ Addr=’” & txtAddr & “’,”
    StrQstr=strQstr & “ Tel=’” & txtTel & “’”
    StrQstr=strQstr & “ Where conID=” & val(sSelID)

    Else If sOperateMode=”Del” Then
    StrQstr=”Delete From tblAddrBook Where conID=” & Val(sSelID)
    Endif
    ‘ Now We will Use the Connection Object’s Execute Method to Run Our Query
    objConn.Execute strSql
    bSaveData=true
    Exit Function

    ErrHandlr:
    Msgbox “Unable to Save The Data Because “ & Vbcrlf & Err.Description , vbinformation, app.title

    End Function

    11. Now We Need to Call the Function to Save The data

    Private cmdSave_Click()
    If bSaveData Then ‘ If data has been Saved Successfully Then Give a message

    Msgbox “Contact has been Added Succefully”
    Call PopulateData ‘ Refreshing the List of already Added data
    End Sub

    12. Now we will be deleting the data Selected by the User

    Private cmdDelete_Click()
    If Val(sSelId)=0 Then ‘Use has not selected any Data to delete
    Msgbox “Please select a Contact to Delete”
    LstContact.setfocous
    Exit Sub
    End if
    If bSaveData Then
    Msgbox “the Contact has been deleted”
    Call ClearControls ‘ Clear the Controls
    Call PopulateData ‘ repopulate The data
    End if

    End Sub

    13. Now we need to write a procedure to populate the data into the list Box.

    Private Function PopulateData()
    Dim dctr As double
    Objrs.activeConnection=ObjConn
    Objrs.LockType=adLockOptimistic
    Objrs.CursorType=adOpenStatic
    Objrs.CursorLocation=adUseClient
    StrQstr=”Select * from tblAddrBook Order By Name”
    Objrs.open StrQstr,,,,adcmdtext
    If objrs.RecordCount>0 Then
    For dCtr=1 to Objrs.Recordcount
    LstContact.addItem Objrs!Name ‘ Displaying Name Only
    ‘Populating ID in the Item Data
    LstContact.ItemData(lstContact.NewIndex)=Objrs!Id ‘
    Objrs.movenext
    Nect dCtr
    Endif
    If objrs.state=1 Then ‘ If Connection is Open
    Objrs.close
    Endif

    End function

    14. Again we need a procedure to Display the Contact Information as the user selects a contact from the List Box

    Private Function DispData()
    Objrs.activeConnection=ObjConn
    Objrs.LockType=adLockOptimistic
    Objrs.CursorType=adOpenStatic
    Objrs.CursorLocation=adUseClient
    StrQstr=”Select * from tblAddrBook Where ID=”& Val(selID)
    Objrs.open StrQstr,,,,adcmdtext
    If objrs.RecordCount>0 Then
    TxtName= Objrs!Name ‘ Displaying Name
    TxtAddr= Objrs!Name
    TxtTel= Objrs!Tel
    Endif
    If objrs.state=1 Then ‘ If Connection is Open
    Objrs.close
    Endif

    End Function

    15. Now We have to write some code to display the detail information in the input controls, when user double clicks a contact in the List box_ Add The Code below into the Double_Click Event of the lstContact

    Private Sub lstContact_DoubleClick(Byval Index as integer)
    SelID=lstContact.itemdata(lstContact.ListIndex)
    Call DispData ‘ Calling Function to display the contact information
    End Sub


    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.

       · Seems like you would at least cover ADO.NET rather than covering 3+ year outdated...
       · contrary to what Microsoft says, there are thousands of ASP coders out there that...
       · but there are already a massive number of ADO tutorials on the web - why continue to...
       · Nice concise information with examples to help a DAO developer move to ADO. Thank...
       · anything i can learn is great, thanks for taking the time to post up the knowlegde!...
     

    DATABASE CODE ARTICLES

    - Deployment of the MobiLink Synchronization M...
    - MobiLink Synchronization Wizard in SQL Anywh...
    - Finding Matching Records in Data Access Pages
    - Using the AccessDataSource Control in VS 2005
    - A Closer Look at ADO.NET: The Command Object
    - A Closer Look at ADO.NET: The Connection Obj...
    - Using ADO to Communicate with the Database, ...
    - Code Snippets: Counting Records
    - Constraints In Microsoft SQL Server 2000
    - Multilingual entries into a DB and to be dis...
    - Getting A List of Tables From SQL Server
    - SQL Server Database Creator - .NET Version
    - ADO Recordset Paging
    - Two combos, one textbox example
    - Discussion & Listserv Module by Mike Eck...

    Iron Speed




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