Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

working with datasets

Posted on 2011-03-11
4
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

722 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