| 9/26/2006
DatabaseFromDataSetAndXMLFile.zip
This article provides the steps
necessary to create a simple database from a DataSet and an XML
file. It will explain how to use a DataSet and an XML file to
implement the database and how to perform CRUD (Create, Retrieve,
Update, and Delete) operations on the data stored in the XML file.
The source code uses the DataSet
created in the tutorial located at
Create Data Table And Columns With
DataSet Designer Visual Basic 2005
To follow along with this article download that source code.
Open the solution created in the
previous article.
Open the solution's 'ExampleOneForm'
in the form designer.

From the Visual Studio menu select
Data -> Show Data Sources.

The Data Sources panel shows the
'MyDataSet' DataSet and the 'Customer' table created in the previous
article.

Drag and drop the 'Customer' table
from the DataSources panel to the surface of 'ExampleOneForm' in the
form designer.

A DataGridView and a BindingNavigator
will be added to the form. Resize the DataGridView and the form to
approximately the size shown in the image below.

Click the BindingNavigator.

A drop down list for adding controls
to the BindingNavigator will appear. Select a Label control from the
drop down list.

A ToolStripLabel control will be added
to the BindingNavigator.

Click the ToolStripLabel control to
select it and use the Properties panel to set its Text property to
'Load'.

Finally, replace the code behind the
'ExampleOneForm' with the code below.
Imports
System.IO
Imports
System.Data
Public
Class ExampleOneForm
Private Sub
DemoOneForm_Load(ByVal sender
As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
' Make this form a MDI child of MainForm.
Me.MdiParent =
My.Forms.MainForm
' Do not allow users to add new rows by
clicking the
' new row at the end of the DataGridView.
Me.CustomerDataGridView.AllowUserToAddRows
= False
' Set the CustomerDataGridView's Dock
property.
Me.CustomerDataGridView.Dock =
DockStyle.Fill
' Maximize the size of this form.
Me.WindowState =
FormWindowState.Maximized
' If the MyDataSet.XML file already exists
enable
' the Load label else disable it.
Me.ToolStripLabel1.Enabled =
File.Exists("MyDataSet.xml")
' Disable the Delete button.
Me.BindingNavigatorDeleteItem.Enabled
= False
End Sub
' Load ToolStripLabel.
Private Sub
ToolStripLabel1_Click(ByVal sender
As System.Object,
ByVal e As
System.EventArgs) Handles
ToolStripLabel1.Click
' Clear the MyDataSet in memory.
Me.MyDataSet.Clear()
' Read the MyDataSet.xml file into the
MyDataSet in memory.
Me.MyDataSet.ReadXml("MyDataSet.xml")
' Disable the Load label and Save Button.
Me.ToolStripLabel1.Enabled =
False
Me.CustomerBindingNavigatorSaveItem.Enabled
= False
End Sub
Private Sub
BindingNavigatorAddNewItem_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
BindingNavigatorAddNewItem.Click
' Enable the Save button.
Me.CustomerBindingNavigatorSaveItem.Enabled
= True
End Sub
Private Sub
BindingNavigatorDeleteItem_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
BindingNavigatorDeleteItem.Click
' If, after a delete, the Customer
DataTable does not have any changes
' disable the Save button.
If
Me.MyDataSet.Customer.GetChanges()
IsNot
Nothing Then
Me.CustomerBindingNavigatorSaveItem.Enabled
= True
Else
Me.CustomerBindingNavigatorSaveItem.Enabled
= False
End If
End Sub
Private Sub
CustomerBindingNavigatorSaveItem_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
CustomerBindingNavigatorSaveItem.Click
' End editing to prepare for a save.
Me.CustomerBindingSource.EndEdit()
Me.CustomerDataGridView.EndEdit()
Try
Me.MyDataSet.WriteXml("MyDataSet.xml",
XmlWriteMode.DiffGram)
Me.CustomerBindingNavigatorSaveItem.Enabled
= False
Catch ex As
Exception
MessageBox.Show("Exception occured: "
& ex.Message, "Save Exception",
MessageBoxButtons.OK)
End Try
End Sub
Private Sub
CustomerDataGridView_CellEndEdit(ByVal
sender As
Object, ByVal e
As
System.Windows.Forms.DataGridViewCellEventArgs)
Handles
CustomerDataGridView.CellEndEdit
' Enable the Save button.
Me.CustomerBindingNavigatorSaveItem.Enabled
= True
End Sub
End
Class
Source CodeClick the link above to download Visual Basic source code in a Visual Studio 2005 solution which demonstrates how to use
a DataSet and an XML file to implement a simple database.
Get Dot Net Code is a web site full of free and pay for use Visual Basic source code for Visual Basic programmers.
|