XML "ini"FIle - How to Detect a Missing Value

E

eBob.com

I've got the basics of an XML ini file working. I.E. I can stash away
and retrieve user preferences. (Code below.) But how do I handle a
new preference? Say I have A and B. And then I invent C. The first
time the new version with C is run the XML file will not have a value
for C. How do I detect that case? Somewhere I got the impression
that I could use IsNull, but I was unable to find an example which I
could apply to my code. My attempt to use IsNull gives a runtime
error:

An unhandled exception of type 'System.MissingMemberException'
occurred in microsoft.visualbasic.dll

Additional information: Public member 'IsNull' on type 'String' not
found.

Here's my code (The runtine error happens in the btnLoad Sub).


Imports System
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

Dim AppDir As String = Directory.GetCurrentDirectory()

#Region " Windows Form Designer generated code "

#End Region

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click
Dim ds As New DataSet("Settings") 'dataset
Dim dt As New DataTable("Textbox") 'table

dt.Columns.Add("xxx_filename", GetType(String)) 'add
columns
dt.Columns.Add("bbb_filename", GetType(String))
dt.Columns.Add("ignore_NOINS", GetType(Boolean))
dt.Columns.Add("log_filename", GetType(String))

dt.Rows.Add(dt.NewRow) 'add a row to table
ds.Tables.Add(dt) 'add table to the dataset

dt.Rows(0)("xxx_filename") = tbxXxxFile.Text 'set
values
dt.Rows(0)("bbb_filename") = tbxBbbFile.Text
dt.Rows(0)("ignore_NOINS") = chkbxIgnoreNOINS.Checked
dt.Rows(0)("log_filename") = tbxLogFile.Text

ds.WriteXml(AppDir & "\testxml.xml") 'write XML file

dt = Nothing
ds = Nothing

End Sub

Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnLoad.Click

Dim ds As New DataSet
Dim dt As DataTable

ds.ReadXml(AppDir & "\testxml.xml") '= ds.Tables(0)

If ds.Tables("Textbox").Rows(0)("xxx_filename").IsNull Then
MsgBox("Whoops, xxx_filename IsNull")
End If

tbxXxxFile.Text = ds.Tables("Textbox").Rows(0)("xxx_filename")
tbxBbbFile.Text = ds.Tables("Textbox").Rows(0)("bbb_filename")
chkbxIgnoreNOINS.Checked =
ds.Tables("Textbox").Rows(0)("ignore_NOINS")
tbxLogFile.Text = ds.Tables("Textbox").Rows(0)("log_filename")

dt = Nothing
ds = Nothing

End Sub
End Class
 
K

Ken Tucker [MVP]

Hi,

ds.Tables("Textbox").Rows(0).IsNull ("xxx_filename")


Ken
----------------
I've got the basics of an XML ini file working. I.E. I can stash away
and retrieve user preferences. (Code below.) But how do I handle a
new preference? Say I have A and B. And then I invent C. The first
time the new version with C is run the XML file will not have a value
for C. How do I detect that case? Somewhere I got the impression
that I could use IsNull, but I was unable to find an example which I
could apply to my code. My attempt to use IsNull gives a runtime
error:

An unhandled exception of type 'System.MissingMemberException'
occurred in microsoft.visualbasic.dll

Additional information: Public member 'IsNull' on type 'String' not
found.

Here's my code (The runtine error happens in the btnLoad Sub).


Imports System
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

Dim AppDir As String = Directory.GetCurrentDirectory()

#Region " Windows Form Designer generated code "

#End Region

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click
Dim ds As New DataSet("Settings") 'dataset
Dim dt As New DataTable("Textbox") 'table

dt.Columns.Add("xxx_filename", GetType(String)) 'add
columns
dt.Columns.Add("bbb_filename", GetType(String))
dt.Columns.Add("ignore_NOINS", GetType(Boolean))
dt.Columns.Add("log_filename", GetType(String))

dt.Rows.Add(dt.NewRow) 'add a row to table
ds.Tables.Add(dt) 'add table to the dataset

dt.Rows(0)("xxx_filename") = tbxXxxFile.Text 'set
values
dt.Rows(0)("bbb_filename") = tbxBbbFile.Text
dt.Rows(0)("ignore_NOINS") = chkbxIgnoreNOINS.Checked
dt.Rows(0)("log_filename") = tbxLogFile.Text

ds.WriteXml(AppDir & "\testxml.xml") 'write XML file

dt = Nothing
ds = Nothing

End Sub

Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnLoad.Click

Dim ds As New DataSet
Dim dt As DataTable

ds.ReadXml(AppDir & "\testxml.xml") '= ds.Tables(0)

If ds.Tables("Textbox").Rows(0)("xxx_filename").IsNull Then
MsgBox("Whoops, xxx_filename IsNull")
End If

tbxXxxFile.Text = ds.Tables("Textbox").Rows(0)("xxx_filename")
tbxBbbFile.Text = ds.Tables("Textbox").Rows(0)("bbb_filename")
chkbxIgnoreNOINS.Checked =
ds.Tables("Textbox").Rows(0)("ignore_NOINS")
tbxLogFile.Text = ds.Tables("Textbox").Rows(0)("log_filename")

dt = Nothing
ds = Nothing

End Sub
End Class
 
E

eBob.com

Thank you Ken, but it did not seem to work. I got ...

An unhandled exception of type 'System.ArgumentException' occurred in
system.data.dll

Additional information: Column 'xxx_filename' does not belong to table
Textbox.

I made the change you gave me. Ran the app, put some data in my text
boxes and hit my Save button. Closed the app. Edited the xml file to
remove the xxx_filename data. Ran the app and hit the Load button.

Here's the complete code with your change ...

Imports System
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

Dim AppDir As String = Directory.GetCurrentDirectory()

#Region " Windows Form Designer generated code "
#End Region

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click
Dim ds As New DataSet("Settings") 'dataset
Dim dt As New DataTable("Textbox") 'table

dt.Columns.Add("xxx_filename", GetType(String)) 'add
columns
dt.Columns.Add("bbb_filename", GetType(String))
dt.Columns.Add("ignore_NOINS", GetType(Boolean))
dt.Columns.Add("log_filename", GetType(String))

dt.Rows.Add(dt.NewRow) 'add a row
to table
ds.Tables.Add(dt) 'add table
to the dataset

dt.Rows(0)("xxx_filename") = tbxXxxFile.Text 'set
values
dt.Rows(0)("bbb_filename") = tbxBbbFile.Text
dt.Rows(0)("ignore_NOINS") = chkbxIgnoreNOINS.Checked
dt.Rows(0)("log_filename") = tbxLogFile.Text

ds.WriteXml(AppDir & "\testxml.xml") 'write
XML file

dt = Nothing
ds = Nothing

End Sub

Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnLoad.Click

Dim ds As New DataSet
Dim dt As DataTable

ds.ReadXml(AppDir & "\testxml.xml") '= ds.Tables(0)

If ds.Tables("Textbox").Rows(0).IsNull("xxx_filename") Then
MsgBox("Whoops, xxx_filename IsNull")
End If

tbxXxxFile.Text = ds.Tables("Textbox").Rows(0)("xxx_filename")
tbxBbbFile.Text = ds.Tables("Textbox").Rows(0)("bbb_filename")
chkbxIgnoreNOINS.Checked =
ds.Tables("Textbox").Rows(0)("ignore_NOINS")
tbxLogFile.Text = ds.Tables("Textbox").Rows(0)("log_filename")

dt = Nothing
ds = Nothing

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top