Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 27th, 2004, 08:40 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding Table to Dataset

I am getting an error trying to add a new table to my dataset, "Object reference not set to an instance of an object". (Usually this means I left out a "New" keyword somewhere.)

Here's what I've got:
Code:
dim ds as New Dataset
Dim myTable As New DataTable
Dim newRow As DataRow

newRow.Item("myItem") = somevalue
myTable.Rows.Add(newRow)
dsResult.Tables.Add("myTable")
Return dsResult
It compiles ok, but I get that error at runtime. I think I am missing a step in here with adding in the Item, but I'm not sure.

Thanks in advance for your comments.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old September 27th, 2004, 09:20 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Like you said, you left out a "new"

Dim newRow As new DataRow

newRow.Item("myItem") = somevalue

However, I think you'll have further problems. You can't just create a new row. You'll need to add columns to the new DataTable, then you create the new row from the table so the row knows what columns are in it. Then you can set values of the row.
 
Old September 27th, 2004, 09:31 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually,
Code:
Dim newRow As new DataRow
gives it's own error. This was something I was pursuing, but I hit a wall trying to add the DataColumn to the DataRow. I don't know of a DataRow method that does this.

That left me with the impression that adding columns to the row was implicit by setting the value, that's why I thought
Code:
newRow.Item("myItem") = somevalue
would work.

So now I'm a bit stymied. I know I want to add a new table to my dataset. Is there a resource out there?

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old September 27th, 2004, 09:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You have to do Dim newRow As DataRow = objTable.NewRow()

The New constructor on DataRow is protected, and therefore cannot be set. You have to use DataTable.NewRow() method. Also, add columns to the DataTable through DataTable.Columns.Add() method.

Then add the table to the dataset by passing the object reference, not just the name:

objDS.Tables.Add(objTable)

Brian
 
Old September 27th, 2004, 09:44 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you both! I found a good reference at http://msdn.microsoft.com/library/de...classtopic.asp

I think it corroborates both your comments.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old September 27th, 2004, 02:06 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just an FYI, here's what I have working:
Code:
Dim myTable As DataTable = New DataTable("myTable")
Dim myCol As DataColumn = avgOneTime.Columns.Add("myCol")

Dim newRow = avgOneTime.NewRow()
newRow.Item("myCol") = somevalue
avgOneTime.Rows.Add(newRow)

dsResult.Tables.Add(myTable)
- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old September 27th, 2004, 02:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Note that you don't have to receive the DataColumn reference using a DataColumn variable, but you can if you need to. I just said that because I saw you passing it to a variable but don't see it used anywhere.

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding row to dataset in C# MAKO C# 2 March 11th, 2010 08:10 AM
adding to a dataset wilbur C# 2 April 16th, 2005 10:44 PM
Adding a new record to a dataset Louisa VB.NET 2002/2003 Basics 1 January 14th, 2004 05:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.