469,903 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Read & Write XML

Hi, I hope to create a XML file that will hold my Connection data to a SQL Db.
I want to write an XML file from 4 Text Box named UserName, Password,
Database & Server and later read from it. My XMLSchema elements are named
dbLogin, dbPassword, dbDatabase & dbServer

I receive a 'System.NullReferenceException Info: Object refernece not set to
an instance of an object' on the following line of code: dtSettings =
Settings.Tables("Settings")

My Code:

Private Sub btnApply_Click(....

Dim dsSettings As DataSet
Settings = New DataSet
Settings = dsSettings

Dim dtSettings As New DataTable
dtSettings = Settings.Tables("Settings")

UserName.DataBindings.Add("Text", dtSettings, "dbLogin")
Password.DataBindings.Add("Text", dtSettings, "dbPassword")
Database.DataBindings.Add("Text", dtSettings, "dbDatabase")
Server.DataBindings.Add("Text", dtSettings, "dbServer")

Dim drNewRow As DataRow
drNewRow = dtSettings.NewRow
drNewRow("dbLogin") = UserName.Text
drNewRow("dbPassword") = Password.Text
drNewRow("dbDatabase") = Database.Text
drNewRow("dbServer") = Server.Text

If dtSettings.Rows.Count() > 1 Then
dtSettings.Rows.RemoveAt(0) ' Remove old row for any
previous users
End If

dtSettings.AcceptChanges()

Dim fs As FileStream = New FileStream(SettingsFileName,
FileMode.Create, FileAccess.ReadWrite)
Dim xtw As XmlTextWriter = New XmlTextWriter(fs,
System.Text.Encoding.Unicode)
Settings.WriteXml(xtw, System.Data.XmlWriteMode.WriteSchema)
xtw.Close()

End Sub

Am I approaching this the correct way. I have my schema set up and the
Nov 21 '05 #1
4 1744
My last thread may have been a bit misleading. I added a New before the
Declaration of dsSettings but then I received a 'System.ArgumentNullException
Info: Value cannot be Null' on the following line of code:
UserName.DataBindings.Add("Text", dtSettings, "dbLogin")

It seems dtSettings IsNothing. I am stumped, please help.
Nov 21 '05 #2
Marcmc,

I assume that you are not using a strongly typed dataset than

I have beneath tried to make code from what you had (in this message).
However what you do is an terrible insecure method. In my opinion should you
never should store usernames and passwords in an XML file.

\\\
Private dsSettings As New DataSet
Private form_load..............
Dim dtSettings As New DataTable
dsSettings.Tables.Add(dtSettings)
dtSettings.Columns("dbLogin",Gettype(system.String ))
dtSettings.Columns("dbPassword"",Gettype(system.St ring))
dtSettings.Columns("dbDatabase"",Gettype(system.St ring))
dtSettings.Columns("dbServer"",Gettype(system.Stri ng))
UserName.DataBindings.Add("Text", dtSettings, "dbLogin")
Password.DataBindings.Add("Text", dtSettings, "dbPassword")
Database.DataBindings.Add("Text", dtSettings, "dbDatabase")
Server.DataBindings.Add("Text", dtSettings, "dbServer")

Dtsettings.Add(dtSettings.NewRow)

ButtonClick event
BindingContext(dsSettings.Tables(0).EndCurrentEdit
dsSettings.WriteXML
///
As your code did not, does this as well not read an XML file by the way.

I hope this helps,

Cor
Nov 21 '05 #3
Your code is a little confusing:
Dim dsSettings As DataSet Here you declare dsSettings as a DataSet. It is still nothing at this
point
Settings = New DataSet Here you assign the Settings variable to a New DataSet... (You don't
need New here since you are going to reassign this variable in the next
line anyway)
Settings = dsSettings But here you discard the new DataSet you created in the previous line
and re-assign it to dsSettings, which is nothing, therefore, Settings
is now nothing.
Dim dtSettings As New DataTable Here you assign dtSettings to a New DataTable. You don't need New on
this line since you are going to assign a reference in the next line.
dtSettings = Settings.Tables("Settings")

And finally here you try do discard the new DataTable that you created
in the previous line and set it to the "Settings" table of the dataset.
But since Settings is still nothing at this point, you get your error.

Hope this helps

Nov 21 '05 #4
Thanks guys I got it with the code below

I have posted a new thread on how to use this configuration file throughout
my application. In other words I dont want to hard code into MyApp the Sql
connection data as i would like it to be as portable as possible. Don't stop
now, I'm nearly there!

Marc
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnApply.Click
If UNameMIS = "admin_mis" Then

' Load & Read the XMLSchema
Dim fileName As String = "C:\Documents and
Settings\marc.mcguckian\My Documents\Visual Studio Projects\MIS RECON
Engine\MISRE_XMLSchema.xsd"

If Not File.Exists(fileName) Then
MessageBox.Show("Schema structure cannot be found")
Return
End If

Dim fs As FileStream = New FileStream(fileName, FileMode.Open,
FileAccess.Read)
Dim xtr As XmlTextReader = New XmlTextReader(fs)
dsSettings = New DataSet
dsSettings.ReadXmlSchema(xtr)
xtr.Close()

' Write to the XMLFile
Dim dtSettings As DataTable = dsSettings.Tables("Settings")
Dim drSettings As DataRow

drSettings = dtSettings.NewRow()
drSettings("dbLogin") = UserName.Text
drSettings("dbPassword") = Password.Text
drSettings("dbDatabase") = Database.Text
drSettings("dbServer") = Server.Text
dtSettings.Rows.Add(drSettings)

dsSettings.AcceptChanges()

Dim SettingsFile As String = txtPollDirectory.Text + "MISRE.xml"
fs = New FileStream(SettingsFile, FileMode.Create,
FileAccess.ReadWrite)
Dim xtw As XmlTextWriter = New XmlTextWriter(fs,
System.Text.Encoding.Unicode)
dsSettings.WriteXml(xtw, System.Data.XmlWriteMode.WriteSchema)
xtw.Close()

sbMIS.Text = "MISRE.xml File Generated"
dbProps.MyUserName = Me.UserName.Text
dbProps.MyPassword = Me.Password.Text
dbProps.MyDatabase = Me.Database.Text
dbProps.MyServer = Me.Server.Text

Me.ClientSize = New System.Drawing.Size(584, 435)
pnlProperties.Visible = False
btnProperties.Enabled = True
btnApply.Visible = False
btnCancel.Visible = False

PollDirectory = txtPollDirectory.Text

Else
sbMIS.Text = "Use buttons to navigate the application (e.g.)
[Properties sets the database and log parameters]."
End If
End Sub
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
9 posts views Thread by Collin VanDyck | last post: by
5 posts views Thread by Arvind P Rangan | last post: by
6 posts views Thread by cj | last post: by
14 posts views Thread by Arne | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.