DataSet DataTable Question

  • Thread starter Thread starter Woody Splawn
  • Start date Start date
W

Woody Splawn

I have a winform with a text field in it. I would like to be able to save
the value in the text field to a XML file so that the next time I open the
Winform I can get the value from the XML file and populate my field. I am
guessing as to exactly what to do but what I have done is as follows:

I created a dataset in my project by right clicking on the project and
selecting add, new dataset. I called it SaveParams01.XSD. There I added an
element, called it CompanyName and called it a string.

I put a button on my form where my intention is to get the value in a
textbox on the form called TextBox1 and then write that value out to and XML
file Called SaveParam.XML. I know how to get the value from the text box
and how to write the dataset to an xml file. The code for doing so I list
below. But I do not know how to do the rest.

Do I need to create a table and somehow associate it with the dataset?
Either to the dataset or to the datatable associated with the dataset I need
to add a row I suppose and assign the value from TextBox1.text to it, but
even after looking at the books I do not know how to write the code to
actually do this.

Could someone help me.


Dim CompanyName As String
CompanyName = TextBox1.Text

???????

DS.WriteXml("SaveParam.xml", XmlWriteMode.WriteSchema)
 
You can use DataBinding directly with the textbox and use a
DataTable/DataSet as the binding object. Check out this link
http://msdn.microsoft.com/library/d...mwindowsformsbindingmanagerbaseclasstopic.asp
from MSDN. Since you can serialize and deserialize datasets very easily
usng ReadXML and WriteXML,it's a cinch just doing it with straight
databinding.

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
I hope you will pardon me but your method of explanation is going to have to
be simpler in order for me to understand. I think that what you are
saying is that if I have a dataset that is bound to a table in a database I
can do binding directly to the field. This is not the case. The TextBox is
just a textbox and is not bound to anything. Not associated with a table in
a database in any way. I am wondering if I can get the value from the text
box and somehow write it to an XML file. If so, how?
 
Hi Woody,

You can try this, I will explain it inline, however today I do not answer
messages anymore so when you have them you have to wait for an answer from
me tomorrow. It is a complete running sample and should normaly run on what
you have made until now.

Private ds As New SaveParams01
' with this you declare your XSD file I assume you have it in your normal
project folder
Private Sub Form7_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim fi As New IO.FileInfo("saveParams01.xml")
'To see if it is the first time you do this the file is in this sample in
the Bin directory
If fi.Exists Then
ds.ReadXml("SaveParams01.xml")
Else
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
'when it does not exist we have to make a row for your information
End If
Me.TextBox1.DataBindings.Add(New Binding("Text", _
ds.Tables(0), "CompanyName"))
'we bind your textbox to the column CompanyName that you have mad (element)
End Sub

Private Sub Form7_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
ds.WriteXml("SaveParams01.xml")
'the only thing we have to do here is write the xml file
End Sub

I hope this helps?

Cor
 
DataBinding deals only locally with data. YOu can bind to a datatable,
dataview, strongly typed collection etc. Anyway, you can bind a textbox to
a given field in a datatable. Since you can read most xml documents with a
DataSet. you can deserialize the XML with
DataSetName.ReadXML("Path:\file.xml) . Now that you have a
dataset/datataable, you can bind it using
TextBoxName.DataBindings.Add("Text", dataSetName.Tables[TableIndex],
"FieldName"); //hwere Text is the property you want to bind to. If you
only have one row's worth of data or 50 you can no flip through the
bindingContext's (the link I sent you previously) Position property and flip
to the next record. Obviously if you only have one record than you won't
have to set up any navigation controls.

Flip through that link I sent you the last time, I think that will help you
in the basics of what I'm getting at. If not, let me know.

Cheers,

Bill

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
Hi Woody,

I forgot to tell you that you should first of cours generate the dataset
rightclicking on your XSD

Here as well the way without databinding, to make it for you more easy to
use without a textbox.

I hope this helps?

Cor
\\\
Private ds As New SaveParams01
Private Sub Form7_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim fi As New IO.FileInfo("SaveParams01.xml")
If fi.Exists Then
ds.ReadXml("SaveParams01.xml")
Else
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
End If
Me.TextBox1.Text = ds.Tables(0).Rows(0)("CompanyName").ToString
End Sub

Private Sub Form7_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
ds.Tables(0).Rows(0)("CompanyName") = Me.TextBox1.Text
ds.WriteXml("SaveParams01.xml")
End Sub
///
 
Your inforamtion was helpful and started me down the path that got me a
solution.

Thank you

Woody
 
Back
Top