Link to home
Start Free TrialLog in
Avatar of sergeiweerasuriya
sergeiweerasuriya

asked on

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?
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

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
In other words, table schema is automatically imported from the XML, and data is automatically populated into the data table.

_______________

  Nayer Naguib
Avatar of sergeiweerasuriya
sergeiweerasuriya

ASKER

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
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()
ASKER CERTIFIED SOLUTION
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No you do not need that part of code anymore.

_______________

  Nayer Naguib