We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

add row and call its fieldname "ID" to dynamically created dataset

jxharding
jxharding asked
on
Medium Priority
191 Views
Last Modified: 2010-04-23
Hi, i want to create a Dataset dynamically,
no problem. i want to add a row called "ID" to my table "MyID"
the problem is adding the row.
i have Row(0) = and this does not work.
i also dont know how to call the row field "ID"

thanks!

        Dim ds As New DataSet
        Dim dt As New System.Data.DataTable("MyID")  
        ds.Tables.Add(dt)

        Dim I As Integer
        For I = 0 To ListView1.Items.Count - 1
            Dim row As DataRow = ds.Tables("MyID").NewRow()
            row(0) = ListView1.Items(I).SubItems(9).Text.Trim.ToUpper
            ds.Tables("MyID").Rows.Add(row)
        Next I

        ds.WriteXml("c:\1.xml")
Comment
Watch Question

Head of Software Services
Commented:
Hi jxharding,

You haven't actually added a column to your datatable:

        Dim ds As New DataSet
        Dim dt As New System.Data.DataTable("MyID")
        dt.Columns.Add("ID")
        ds.Tables.Add(dt)

        Dim I As Integer
        For I = 0 To  ListView1.Items.Count - 1
            Dim row As DataRow = ds.Tables("MyID").NewRow()
            row.Item("ID") =  ListView1.Items(I).SubItems(9).Text.Trim.ToUpper
            ds.Tables("MyID").Rows.Add(row)
        Next I

        ds.WriteXml("c:\1.xml")

This adds a column called ID to the datatable, then you can refer to this by name as row.Item("ID")

Tim Cottee

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

Ask the Experts
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.