Solved

working with datasets

Posted on 2011-03-11
4
272 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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