We help IT Professionals succeed at work.

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

sergeiweerasuriya
on
Medium Priority
235 Views
Last Modified: 2010-04-17
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?
Comment
Watch Question

You did not add the DataTable to the DataSet. :-)

_______________

  Nayer Naguib

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
what is the code to do that and where should i write it?

Author

Commented:
oh yes i added ds.Tables.Add(dtCus) and now it's working. thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.