Solved

working with datasets

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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 …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

626 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