Adding a new Row - Data Sets - ADO.NET

As a part of an academic project i'm developing an electricity meter reader. It consists of a pocket pc application as well as the desktop application. VB.Net is the programming language. We've been told that there's no need to create a DB for the desktop application and instead use DataSets and pass the data to XML. The same approach will be taken for the Pocket pc application. When we plugin the pocket pc application to the Desktop pc, these XML files will be automatically synchronised using Microsoft Active Sysnc. We do not have to write any code for the synchronisation. Now in the Desktop application i have only 1 form. The data that needs to be stored are (Customer first name, Customer last name, Tel No, Address Line1, Address Line 2, Post Code, Day-time Reading, Night-time Reading). On this form i should be able to Add new Customers. The only data that will be passed to the XML file in the Pocket pc application is Day-time Reading and the Night-time Reading.

When adding new Customers obviously i have to append that data to the XML file, isn't it?

So my understanding is, I Read the already synchronised XML file on the Desktop application to the DataSet using ReadXml method, then populate the Dataset and append the new row to the DataSet which will then be passed to the XML file using WriteXml method. Am i correct?

When doing this do i need to define a structure to the DataSet or when i Read the XML file and populate it to the DataSet does it automatically define the DataSet structure and populate it?
sergeiweerasuriyaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nayernaguibCommented:
Yes, when you read the XML file using ReadXML() method, the schema is defined, and the data table is populated. You can then add/edit/delete data, and use WriteXML() to save data back.

And about populating the DataSet, you do not need to define the schema of the data manually. This is stored in the XML file.

_______________

  Nayer Naguib
0
nayernaguibCommented:
In other words, table schema is automatically imported from the XML, and data is automatically populated into the data table.

_______________

  Nayer Naguib
0
sergeiweerasuriyaAuthor Commented:
ok now below is the code for adding a record to the XML file. I added the very first record to the xml file. now how do i go about adding more records.

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        ' creating a DataSet
        Dim ds As DataSet = New DataSet

        'Creating an empty DataTable
        Dim dtCus As New DataTable("Customers")

        'Creating  8 data columns
        Dim dcolCus_fname As New DataColumn("Cus_fname")
        dcolCus_fname.DataType = GetType(String)
        Dim dcolCus_lname As New DataColumn("Cus_lname")
        dcolCus_lname.DataType = GetType(String)
        Dim dcolTel As New DataColumn("Tel")
        dcolTel.DataType = GetType(String)
        Dim dcolAdd_line1 As New DataColumn("Add_Line1")
        dcolAdd_line1.DataType = GetType(String)
        Dim dcolAdd_line2 As New DataColumn("Add_Line2")
        dcolAdd_line2.DataType = GetType(String)
        Dim dcolpost_code As New DataColumn("Post_Code")
        dcolpost_code.DataType = GetType(String)
        Dim dcolday_read As New DataColumn("Day_Read")
        dcolday_read.DataType = GetType(String)
        Dim dcolnight_read As New DataColumn("Night_Read")
        dcolnight_read.DataType = GetType(String)

        'Adding the columns to the DataTable
        dtCus.Columns.Add("Cus_fname")
        dtCus.Columns.Add("Cus_lname")
        dtCus.Columns.Add("Tel")
        dtCus.Columns.Add("Add_Line1")
        dtCus.Columns.Add("Add_Line2")
        dtCus.Columns.Add("Post_Code")
        dtCus.Columns.Add("Day_Read")
        dtCus.Columns.Add("Night_Read")

        'Adding rows to the DataTable
        Dim row As DataRow
        row = dtCus.NewRow()

        row("Cus_fname") = txtCustomerFirstName.Text
        row("Cus_lname") = txtCustomerLastName.Text
        row("Tel") = txtTelNo.Text
        row("Add_Line1") = txtAddressLine1.Text
        row("Add_Line2") = txtAddressLine2.Text
        row("Post_Code") = txtPostcode.Text
        row("Day_Read") = txtDayRead.Text
        row("Night_Read") = txtNightRead.Text
        dtCus.Rows.Add(row)

        ds.Tables.Add(dtCus)

        ds.WriteXml("Customers.xml")

  End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sergeiweerasuriyaAuthor Commented:
fro this point onward i do not need the code below. Do i?

Dim dcolCus_fname As New DataColumn("Cus_fname")
        dcolCus_fname.DataType = GetType(String)
        Dim dcolCus_lname As New DataColumn("Cus_lname")
        dcolCus_lname.DataType = GetType(String)
        Dim dcolTel As New DataColumn("Tel")
        dcolTel.DataType = GetType(String)
        Dim dcolAdd_line1 As New DataColumn("Add_Line1")
        dcolAdd_line1.DataType = GetType(String)
        Dim dcolAdd_line2 As New DataColumn("Add_Line2")
        dcolAdd_line2.DataType = GetType(String)
        Dim dcolpost_code As New DataColumn("Post_Code")
        dcolpost_code.DataType = GetType(String)
        Dim dcolday_read As New DataColumn("Day_Read")
        dcolday_read.DataType = GetType(String)
        Dim dcolnight_read As New DataColumn("Night_Read")
        dcolnight_read.DataType = GetType(String)

        'Adding the columns to the DataTable
        dtCus.Columns.Add("Cus_fname")
        dtCus.Columns.Add("Cus_lname")
        dtCus.Columns.Add("Tel")
        dtCus.Columns.Add("Add_Line1")
        dtCus.Columns.Add("Add_Line2")
        dtCus.Columns.Add("Post_Code")
        dtCus.Columns.Add("Day_Read")
        dtCus.Columns.Add("Night_Read")

        'Adding rows to the DataTable
        Dim row As DataRow
        row = dtCus.NewRow()
0
nayernaguibCommented:
You need to add code like this:

        Dim ds As New DataSet
        ds.ReadXml("Customers.xml")
        Dim dr As DataRow = ds.Tables("Customers").NewRow()
        dr("Cus_fname") = "myFirstName"
        dr("Cus_lname") = "myLastName"
        'add here other column values
        ds.Tables("Customers").Rows.Add(dr)
        ds.WriteXml("Customers.xml")

_______________

  Nayer Naguib

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nayernaguibCommented:
No you do not need that part of code anymore.

_______________

  Nayer Naguib
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.