473,785 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.NullRef erenceException 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.DataBi ndings.Add("Tex t", dtSettings, "dbLogin")
Password.DataBi ndings.Add("Tex t", dtSettings, "dbPassword ")
Database.DataBi ndings.Add("Tex t", dtSettings, "dbDatabase ")
Server.DataBind ings.Add("Text" , dtSettings, "dbServer")

Dim drNewRow As DataRow
drNewRow = dtSettings.NewR ow
drNewRow("dbLog in") = UserName.Text
drNewRow("dbPas sword") = Password.Text
drNewRow("dbDat abase") = Database.Text
drNewRow("dbSer ver") = Server.Text

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

dtSettings.Acce ptChanges()

Dim fs As FileStream = New FileStream(Sett ingsFileName,
FileMode.Create , FileAccess.Read Write)
Dim xtw As XmlTextWriter = New XmlTextWriter(f s,
System.Text.Enc oding.Unicode)
Settings.WriteX ml(xtw, System.Data.Xml WriteMode.Write Schema)
xtw.Close()

End Sub

Am I approaching this the correct way. I have my schema set up and the
Nov 21 '05 #1
4 1939
My last thread may have been a bit misleading. I added a New before the
Declaration of dsSettings but then I received a 'System.Argumen tNullException
Info: Value cannot be Null' on the following line of code:
UserName.DataBi ndings.Add("Tex t", 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.Tabl es.Add(dtSettin gs)
dtSettings.Colu mns("dbLogin",G ettype(system.S tring))
dtSettings.Colu mns("dbPassword "",Gettype(syst em.String))
dtSettings.Colu mns("dbDatabase "",Gettype(syst em.String))
dtSettings.Colu mns("dbServer"" ,Gettype(system .String))
UserName.DataBi ndings.Add("Tex t", dtSettings, "dbLogin")
Password.DataBi ndings.Add("Tex t", dtSettings, "dbPassword ")
Database.DataBi ndings.Add("Tex t", dtSettings, "dbDatabase ")
Server.DataBind ings.Add("Text" , dtSettings, "dbServer")

Dtsettings.Add( dtSettings.NewR ow)

ButtonClick event
BindingContext( dsSettings.Tabl es(0).EndCurren tEdit
dsSettings.Writ eXML
///
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.EventArg s) Handles btnApply.Click
If UNameMIS = "admin_mis" Then

' Load & Read the XMLSchema
Dim fileName As String = "C:\Documen ts and
Settings\marc.m cguckian\My Documents\Visua l Studio Projects\MIS RECON
Engine\MISRE_XM LSchema.xsd"

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

Dim fs As FileStream = New FileStream(file Name, FileMode.Open,
FileAccess.Read )
Dim xtr As XmlTextReader = New XmlTextReader(f s)
dsSettings = New DataSet
dsSettings.Read XmlSchema(xtr)
xtr.Close()

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

drSettings = dtSettings.NewR ow()
drSettings("dbL ogin") = UserName.Text
drSettings("dbP assword") = Password.Text
drSettings("dbD atabase") = Database.Text
drSettings("dbS erver") = Server.Text
dtSettings.Rows .Add(drSettings )

dsSettings.Acce ptChanges()

Dim SettingsFile As String = txtPollDirector y.Text + "MISRE.xml"
fs = New FileStream(Sett ingsFile, FileMode.Create ,
FileAccess.Read Write)
Dim xtw As XmlTextWriter = New XmlTextWriter(f s,
System.Text.Enc oding.Unicode)
dsSettings.Writ eXml(xtw, System.Data.Xml WriteMode.Write Schema)
xtw.Close()

sbMIS.Text = "MISRE.xml File Generated"
dbProps.MyUserN ame = Me.UserName.Tex t
dbProps.MyPassw ord = Me.Password.Tex t
dbProps.MyDatab ase = Me.Database.Tex t
dbProps.MyServe r = Me.Server.Text

Me.ClientSize = New System.Drawing. Size(584, 435)
pnlProperties.V isible = False
btnProperties.E nabled = True
btnApply.Visibl e = False
btnCancel.Visib le = False

PollDirectory = txtPollDirector y.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
6826
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
9
8580
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 reference (i.e.  ) and it seems to be converting the character to its UNICODE representation. Take this source XML document: <?xml version="1.0" encoding="UTF-8"?>
0
1533
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
2031
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. Pls if anyone knows the link or example let me know Thanks ARvind.
8
2818
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);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
6
2246
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 = doc.GetElementsByTagName("co_name") textbox1.text = co_name(0).innertext Now I'm getting company names that have ampersands in them. I was not aware that was not allowed in xml and had no method of dealing with it.
0
1166
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 tables t1 & t2. C2 is remote and has a host HB also with X.db's tables t1 & t2. Controller CR has 2 controllers under it - C3 & C4. C3 is local and has a host HC with X.db's tables t1 & t2. C4 is remote and has a host HD also with X.db's tables t1 &...
14
5935
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
0
1900
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++ code. The result write exactly but it has funny-looking unwanted characters attached at the end of the .bin file. How do I fix it? #include <iostream> #include <fstream> using namespace std; struct details { int id;
2
7701
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 file then the writed html file cannnot show the japanese character. my new created html file show the another character, it cannot show the original japanese character. But when i read & write data from a txt file it works. But only problem is,When...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.