Dear All
I have the following code, which when executed, is returning the following error:-
Object reference not set to an instance of an object
'A form that allows the user to open an XML file and edit its contents on a Datagrid.
'Then the user can save his changes back to the file
Imports System.Data
Imports System.Xml
Public Class GuidedPracticeExe2_1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents dgXML As System.Windows.Forms.DataGrid
Friend WithEvents btnLoadXml As System.Windows.Forms.Button
Friend WithEvents btnSaveXML As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.dgXML = New System.Windows.Forms.DataGrid
Me.btnLoadXml = New System.Windows.Forms.Button
Me.btnSaveXML = New System.Windows.Forms.Button
CType(Me.dgXML, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'dgXML
'
Me.dgXML.DataMember = ""
Me.dgXML.Dock = System.Windows.Forms.DockStyle.Bottom
Me.dgXML.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgXML.Location = New System.Drawing.Point(0, 78)
Me.dgXML.Name = "dgXML"
Me.dgXML.Size = New System.Drawing.Size(720, 384)
Me.dgXML.TabIndex = 9
'
'btnLoadXml
'
Me.btnLoadXml.Location = New System.Drawing.Point(264, 24)
Me.btnLoadXml.Name = "btnLoadXml"
Me.btnLoadXml.TabIndex = 8
Me.btnLoadXml.Text = "Load XML"
'
'btnSaveXML
'
Me.btnSaveXML.Location = New System.Drawing.Point(368, 24)
Me.btnSaveXML.Name = "btnSaveXML"
Me.btnSaveXML.TabIndex = 10
Me.btnSaveXML.Text = "Save XML"
'
'GuidedPracticeExe2_1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(720, 462)
Me.Controls.Add(Me.btnSaveXML)
Me.Controls.Add(Me.dgXML)
Me.Controls.Add(Me.btnLoadXml)
Me.Name = "GuidedPracticeExe2_1"
Me.Text = "GuidedPracticeExe2_1"
CType(Me.dgXML, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Dim xdd As XmlDataDocument
Dim ds As DataSet
Private Sub btnLoadXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadXml.Click
xdd = New XmlDataDocument
'Create a new XMLTextReader on the file
Dim xtr As XmlTextReader = New XmlTextReader("..\Books.xml")
'Initialize the dataset by reading the schema from the XML doc
ds.ReadXmlSchema(xtr)
'Reset the XMLTextReader
xtr.Close()
xtr = New XmlTextReader("..\Books.xml")
'Tell it to ignore whitespace
xtr.WhitespaceHandling = WhitespaceHandling.None
'LOad the synchronized object
xdd.Load(xtr)
'Display the resultant dataset
dgXML.DataSource = ds
dgXML.DataMember = "Book"
'Clean Up
xtr.Close()
End Sub
Private Sub GuidedPracticeExe2_1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Retreive the associated dataset and store it
ds = xdd.DataSet()
End Sub
Private Sub btnSaveXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
'Create a new XmlTextWriter on the file
Dim xtw As XmlTextWriter = New XmlTextWriter("..\Books.xml", _
System.Text.Encoding.UTF8)
'write the doc to it
xdd.WriteTo(xtw)
xtw.Close()
End Sub
End Class
Can anybody help me please?
Thanks for your help and time
Johann
I have the following code, which when executed, is returning the following error:-
Object reference not set to an instance of an object
'A form that allows the user to open an XML file and edit its contents on a Datagrid.
'Then the user can save his changes back to the file
Imports System.Data
Imports System.Xml
Public Class GuidedPracticeExe2_1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents dgXML As System.Windows.Forms.DataGrid
Friend WithEvents btnLoadXml As System.Windows.Forms.Button
Friend WithEvents btnSaveXML As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.dgXML = New System.Windows.Forms.DataGrid
Me.btnLoadXml = New System.Windows.Forms.Button
Me.btnSaveXML = New System.Windows.Forms.Button
CType(Me.dgXML, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'dgXML
'
Me.dgXML.DataMember = ""
Me.dgXML.Dock = System.Windows.Forms.DockStyle.Bottom
Me.dgXML.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgXML.Location = New System.Drawing.Point(0, 78)
Me.dgXML.Name = "dgXML"
Me.dgXML.Size = New System.Drawing.Size(720, 384)
Me.dgXML.TabIndex = 9
'
'btnLoadXml
'
Me.btnLoadXml.Location = New System.Drawing.Point(264, 24)
Me.btnLoadXml.Name = "btnLoadXml"
Me.btnLoadXml.TabIndex = 8
Me.btnLoadXml.Text = "Load XML"
'
'btnSaveXML
'
Me.btnSaveXML.Location = New System.Drawing.Point(368, 24)
Me.btnSaveXML.Name = "btnSaveXML"
Me.btnSaveXML.TabIndex = 10
Me.btnSaveXML.Text = "Save XML"
'
'GuidedPracticeExe2_1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(720, 462)
Me.Controls.Add(Me.btnSaveXML)
Me.Controls.Add(Me.dgXML)
Me.Controls.Add(Me.btnLoadXml)
Me.Name = "GuidedPracticeExe2_1"
Me.Text = "GuidedPracticeExe2_1"
CType(Me.dgXML, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Dim xdd As XmlDataDocument
Dim ds As DataSet
Private Sub btnLoadXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadXml.Click
xdd = New XmlDataDocument
'Create a new XMLTextReader on the file
Dim xtr As XmlTextReader = New XmlTextReader("..\Books.xml")
'Initialize the dataset by reading the schema from the XML doc
ds.ReadXmlSchema(xtr)
'Reset the XMLTextReader
xtr.Close()
xtr = New XmlTextReader("..\Books.xml")
'Tell it to ignore whitespace
xtr.WhitespaceHandling = WhitespaceHandling.None
'LOad the synchronized object
xdd.Load(xtr)
'Display the resultant dataset
dgXML.DataSource = ds
dgXML.DataMember = "Book"
'Clean Up
xtr.Close()
End Sub
Private Sub GuidedPracticeExe2_1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Retreive the associated dataset and store it
ds = xdd.DataSet()
End Sub
Private Sub btnSaveXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
'Create a new XmlTextWriter on the file
Dim xtw As XmlTextWriter = New XmlTextWriter("..\Books.xml", _
System.Text.Encoding.UTF8)
'write the doc to it
xdd.WriteTo(xtw)
xtw.Close()
End Sub
End Class
Can anybody help me please?
Thanks for your help and time
Johann