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

Event for the Save Button
The event for the Save button reads the values from the text boxes and stores them in the current record in the data table. The event for the Save Button is:

    Private Sub btnSave_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnSave.Click
        lblLabel.Text = "Saving Changes..." 
        Me.Cursor = Cursors.WaitCursor
        Dim row As DataRow = dataTable.Rows(currRec)
        row.BeginEdit()
        row("title") = txtArticleTitle.Text 
        row("topic") = txtArticleTopic.Text
        row("authorId") = txtAuthorId.Text
        row("lines") = txtNumOfLines.Text
        row("dateOfPublishing") = txtDateOfPublishing.Text
        row.EndEdit()
        dataAdapter.Update(ds, "article")
        ds.AcceptChanges()
        ToggleControls(True)
        insertSelected = False
        Me.Cursor = Cursors.Default
        lblLabel.Text = "Changes Saved"
    End Sub
Here we first change the progress label (lblLabel) text to show the current status and then change the cursor to a wait cursor.

lblLabel.Text = "Saving Changes...
"Me.Cursor = Cursors.WaitCursor
Then we take a reference of the current record's row and call its BeginEdit() method. An update on a row is usually bound by a DataRow's BeginEdit() and EndEdit() methods. The BeginEdit() method temporarily suspends the events for the validation of row's data. Within the BeginEdit() and EndEdit() boundary, we stored the changed values in the text boxes to the row.

Dim row As DataRow = dataTable.Rows(currRec)
row.BeginEdit()
row("title") = txtArticleTitle.Text
row("topic") = txtArticleTopic.Text
row("authorId") = txtAuthorId.Text
row("lines") = txtNumOfLines.Text
row("dateOfPublishing") = txtDateOfPublishing.Text
row.EndEdit()

After saving the changes in the row, we update the DataSet and table by calling the Update method of the Data Adapter. This saves the changes in the local repository of data: DataSet. To save the changed rows and tables to the physical database, we called the AcceptChanges() method of the DataSet class.

dataAdapter.Update(ds, "article")
ds.AcceptChanges()
Finally we brought the controls to the normal mode by calling the ToggleControls() method and passing it the true value. We set the insertSelected variable to false, changed the cursor back to normal and updated the progress label. (The use of the insertSelected variable is discussed later in the Cancel button event )

ToggleControls(True)
insertSelected = False
Me.Cursor = Cursors.Defaultlbl
Label.Text = "Changes Saved"
It is important to note here that the Save button is used to save the changes in the current record. It may be selected after either the Edit Record or Insert Record buttons. In the case of the Edit Record button, the pointer is already on the current record, while in the case of the Insert Record button, as we will see shortly in the Inserting Record section, the program inserts a new empty row. Then moves the current record pointer to it and presents it to the user to insert the values. Hence, the job of the Save button in both cases is to save the changes in the current record from the text boxes to the data table, updating the Data Set and finally updating the database.

Event for the Cancel Button
The Cancel button's event handler is:

    Private Sub btnCancel_Click(ByVal sender As System.Object, 
		ByVal e As System.EventArgs) Handles btnCancel.Click
        If insertSelected Then
            btnDeleteRecord_Click(Nothing, Nothing)
            insertSelected = False
        End If
        FillControls()
        ToggleControls(True)
    End Sub
The form and controls can be brought into edit mode by pressing either the Edit Record or the Insert Record button. When the Insert Record button is selected, the program inserts an empty record (row) to the table (the details of which we will see shortly) and brings the form and controls to edit mode by using the ToggleControls(false) statement. The Cancel button is used to cancel both editing of the current record and the newly inserted record. When canceling the insertion of a new record, the program needs to delete the current (newly inserted) row from the table. We have used a Boolean variable 'insertSelected' in our application, which is set to true when the user selects the Insert Record button. This Boolean value informs the Cancel button whether the edit mode was set by the Edit Record button or by the Insert Record button. Hence the Cancel button's event first checks whether insertSelected is true and if it is, it calls the Delete button's event to delete the current record and sets insertSelected back to false.

If insertSelected Then
    btnDeleteRecord_Click(Nothing, Nothing)
    insertSelected = False
End If
Now fill the controls (text boxes) from the data in the current record and bring the controls back to the normal mode.

FillControls()
ToggleControls(True)
Inserting Records
To insert a record into the table, the user can select the Insert Record button. A record is inserted into the table by adding a new row to the DataTable's Rows collection. Here is the event for the Insert Record button.

    Private Sub btnInsertRecord_Click(ByVal sender As System.Object, _ 
               ByVal e As System.EventArgs) Handles btnInsertRecord.Click
        insertSelected = True
        Dim row As DataRow = dataTable.NewRow()
        dataTable.Rows.Add(row)
        totalRec = dataTable.Rows.Count
        currRec = totalRec - 1
        row("artId") = totalRec
        txtArticleId.Text = totalRec.ToString()
        txtArticleTitle.Text = ""
        txtArticleTopic.Text = ""
        txtAuthorId.Text = ""
        txtNumOfLines.Text = ""
        txtDateOfPublishing.Text = DateTime.Now.Date.ToString()
        ToggleControls(False)
    End Sub
First of all we set the insertSelected variable to true, so that later the Cancel button may get informed that the edit mode was set by the Insert Record button. We then created a new DataRow using the DataTable's NewRow() method. Then we added it to the Rows collection of the data table and updated the currRec and totalRec variables.

Dim row As DataRow = dataTable.NewRow() 
dataTable.Rows.Add(row)
totalRec = dataTable.Rows.Countcurr
Rec = totalRec - 1
The new row is ready to have the new values inserted into it. Here we have set the artId field to the total number of records as we don't want to allow the user to set the primary key field of the table. Of course this is just a design issue. You may want to allow your user to insert the primary key field value too.

row("artId") = totalRec
txtArticleId.Text = totalRec.ToString()
We then cleared all the text boxes, but filled the Date of the Publishing text box with the current date in order to help the user, and finally set the edit mode by calling the ToggleControls() method.

txtArticleTitle.Text = ""
txtArticleTopic.Text = ""
txtAuthorId.Text = ""
txtNumOfLines.Text = ""
txtDateOfPublishing.Text = DateTime.Now.Date.ToString()
ToggleControls(False)
Deleting a Record
Deleting a record is again very simple. All you need to do is get a reference to the target row and call its Delete() method. Then you need to call the Update() method of the data adapter and the AcceptChanges() method of the DataSet to permanently save your changes in the data table to the physical database. The event handler for the Delete Record button is:

    Private Sub btnDeleteRecord_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnDeleteRecord.Click
        Dim res As DialogResult = MessageBox.Show( _
                   "Are you sure you want to delete the current record?", _
                   "Confirm Record Deletion", MessageBoxButtons.YesNo)
        If res = DialogResult.Yes Then
            Dim row As DataRow = dataTable.Rows(currRec)
            row.Delete()
            dataAdapter.Update(ds, "article")
            ds.AcceptChanges()
            lblLabel.Text = "Record Deleted"
            totalRec -= 1
            currRec = totalRec - 1
            FillControls()
        End If
    End Sub
Since selecting the delete record button will permanently delete the current record, we need to seek confirmation from the user to check they are sure they wish to go ahead with the deletion. For this purpose we present the user with a message box with Yes and No buttons.

Dim res As DialogResult = MessageBox.Show( _
           "Are you sure you want to delete the current record?", _
           "Confirm Record Deletion", MessageBoxButtons.YesNo)
If the user selects the Yes button in the message box, the code to delete the current record is executed.

If res = DialogResult.Yes Then
Dim row As DataRow = dataTable.Rows(currRec)
    row.Delete()
    dataAdapter.Update(ds, "article")
    ds.AcceptChanges()
    lblLabel.Text = "Record Deleted"
    totalRec -= 1
    currRec = totalRec - 1
    FillControls()
End If
First we got a reference to the row representing the current record in the data table and called its Delete() method. We then saved the changes to the database and updated the totalRec and currRec variables. Finally, we filled the controls (text boxes) with the last record in the table. This concludes our demonstration application to perform common data access tasks using ADO.NET. The complete source code of this program can be downloaded by clicking here


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.