Solved

working with datasets

Posted on 2011-03-11
4
273 Views
Last Modified: 2012-05-11
I get a error (Object reference not set to an instance of an object.) in the attached code on this line:

dt.Columns.Add(New DataColumn("Path"))


Dim DS As New DataSet()
            Dim dt As DataTable = DS.Tables("Category")


            For i = 1 To col.Count
                cID = col.Item(i).ToString()

            

                dt.Columns.Add(New DataColumn("Path"))
                'dt.Columns.Add(New DataColumn("ParentName"))

                dt.Rows(0)("Path") = "/" & ReturnCategoryPath(cID)

            Next

Open in new window

0
Comment
Question by:vbnetcoder
  • 2
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 35113354
Your DataTable doesn't yet exist because you have only just created a new DataSet (which has no DataTables to begin with).

I assume you want to add a DataTable "Category" to your DataSet? If so, then you first need to create that DataTable and add it to your DataSet....

        Dim DS As New DataSet()
        Dim dt As New DataTable("Category")
        DS.Tables.Add(dt)

        For i = 1 To col.Count
                cID = col.Item(i).ToString()            

                dt.Columns.Add("Path")
                dt.Columns.Add("ParentName")

                dt.Rows(0)("Path") = "/" & ReturnCategoryPath(cID)

        Next

Open in new window


Wayne
0
 

Author Comment

by:vbnetcoder
ID: 35113430
Now, i get a error at

dt.Rows(0)("Path") = "/" & ReturnCategoryPath(cID)

The error is:

There is no row at position 0.
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 35113483
Ahh, yes, a newly created table also has no DataRows, so you'll have to create them and add them to the table...

        Dim DS As New DataSet()
        Dim dt As New DataTable("Category")
        DS.Tables.Add(dt)

        For i = 1 To col.Count
                cID = col.Item(i).ToString()            

                dt.Columns.Add("Path")
                dt.Columns.Add("ParentName")

                Dim dr As DataRow = dt.NewRow()
                dr("Path") = "/" & ReturnCategoryPath(cID)
                
                dt.Rows.Add(dr)

        Next

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 35113512
ty
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question