Solved

working with datasets

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now