Read & Write XML

G

Guest

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
 
G

Guest

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.
 
C

Cor Ligthert [MVP]

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.String))
dtSettings.Columns("dbDatabase"",Gettype(system.String))
dtSettings.Columns("dbServer"",Gettype(system.String))
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
 
C

Chris Dunaway

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
 
G

Guest

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
 

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

Similar Threads


Top