Link to home
Start Free TrialLog in
Avatar of sergeiweerasuriya
sergeiweerasuriya

asked on

Adding a record to the dataset and passing it to the XML file - ADO.NET

When i fill in the form and click on btnAdd it should add that record to a XML file. BUT IT DOESN'T DO SO. When i opened the xml file all i can see is : <?xml version="1.0" standalone="yes" ?>
  <NewDataSet />

The code is given below.

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.WriteXml("Customers.xml")

What is the problem?
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
Avatar of sergeiweerasuriya
sergeiweerasuriya

ASKER

what is the code to do that and where should i write it?
oh yes i added ds.Tables.Add(dtCus) and now it's working. thanks.