473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1920
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
0
by: Abubakar | last post by:
Hi, try { int x = ns.Read(readbuffer, 0, readbuffer.Length); } catch (System.IO.IOException ioexception) { UINotifications.ServerMessageDisplay(ioexception.ToString( )); }
5
by: Arvind P Rangan | last post by:
Hi, i like to read an existing xml file which has a schema defined to it, and then write or add data to the existing xml file using vb.net/c#. May be this Question has been answered earlier....
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
6
by: cj | last post by:
I'm receiving an xml formatted string that I pull data from by reading it into an xml document like this: Dim doc As New Xml.XmlDocument doc.LoadXml(respstr) Dim co_name As Xml.XmlNodeList =...
0
by: rchadda | last post by:
I haven't started using Seqouia/C-jdbc but I am probing the ability to do something like this: Controller CW has 2 controllers under it - C1 & C2. C1 is local and has a host HA with X.db's...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
0
by: magicbeans | last post by:
I am trying to create a .bin file of records from a .txt file. Initially the records are stored in a text file and I want to Read from the .txt file and Write to the .bin file. This is the C++...
2
by: Bina | last post by:
hi, I want to read & write a html file which contain the japanese character. Now how i will do it?I can read & write english character . but when i read & write Japanese character from the html...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.