Programmer's Heaven - For C C++ Pascal Delphi Visual Basic Assembler C# .Net java JSP ASP ASP.NET Javascript developers!
Members
Username:

Password:

Auto-login

Register
Why register?
Forgot Password?
Message Boards
FAQ 
CodePedia
Free Magazines
Sample Chapters
User search
What's New
Top lists
RSS Feeds RSS Feed

Submit content
Contact Us
Link To Us
Help



Advanced Search
Newsletter
E-mail:


More information


Previous Page Next Page

Navigating through the records
Navigating through the records is again very easy. For the "Next" button, we have written the following simple event.

Private Sub btnNext_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnNext.Click
        currRec += 1
        If currRec >= totalRec Then
        	currRec = 0
        End If
        FillControls()
End Sub
We first increment the integer variable currRec and check if it has crossed the last record (using the totalRec variable) in the table. If it has, then we move the current record to the first record. We then call the FillControls() method to display the current record on the form.

Similarly the event for the "Previous" button looks like this:

Private Sub btnPrevious_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnPrevious.Click
        currRec -= 1
        If currRec < totalRec Then
            currRec = totalRec - 1
        End If
        FillControls()
End Sub
Here we decrement the currRec variable and check if has crossed the first record and if it has then we move to the last record. Once again, we call the FillControls() method to display the current record.

You can now navigate through the records using the Next and Previous buttons.

Updating the table
So far we have only selected the data from the database and haven't changed any rows, inserted new rows or deleted existing rows. Let's learn how to perform these tasks one by one.

Please note that for this section, we will only use the "Article" table and will not be using the "Author" table for the sake of simplicity.

Steps for updating the table
Updating a table in ADO.NET is very interesting and easy. You need to follow these steps to update, insert and delete records:

  • The Data Adapter class (SqlDataAdapter) has properties for each of the insert, update and delete commands. First of all we need to prepare the command (SqlCommand) and add it to the Data Adapter object. The commands are simple SQL commands with parameters. To implement this step, we will introduce a method InitializeCommands() in the following example.
  • Secondly we need to add parameters to these commands. The parameters are simply the names of the data table fields involved in the particular commands. To implement this step, we will introduce a method named AddParams() in the following example.
  • The two steps described above are only done once in the application. For each insert, update and delete; we insert, update and delete the corresponding Data Row (DataRow) of the Data Table (DataTable) object.
  • After an update we call the Update() method of the Data Adapter class by supplying to it, the DataSet and table name as parameters. This updates our local DataSet.
  • Finally we call the AcceptChanges() method of the DataSet object to store the changes in the DataSet to the physical database.

Again for simplicity we haven't included the steps for data validation, which is a key part of a data update in a "live" application.

Building the Application
The application will finally look like this:

In this application, we have defined several database access objects (like SqlDataAdapter, DataSet) as private class members so that we can access them in different methods.

Public Class ADOForm    
	Inherits System.Windows.Forms.Form    
	' Private global members to be used in various methods    
    	Private conn As SqlConnection    
	Private conn As SqlConnection
	Private ds As DataSet    
	Private dataTable As dataTable   
	Private currRec As Integer = 0    
	Private totalRec As Integer = 0    
	Private insertSelected As Boolean    
	...


Previous Page Next Page


 

Advertisment

 
Partners:
ASP Alliance
Code Project
Developers Dex
Developer Fusion
DevGuru
Planet Source Code
Tek-Tips Forums
More Partners:
Spain Travel Guide
Discount Hotels
Personal Injury Claims UK
Price Comparison
More Partners
Web Site Services
Domain Names UK
UK Domain Registration
 

Newsletter Submit Content About Advertising Awards Contact Us Link to us    
© 1996-2005 128K-Communications Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data.