PC Review


Reply
Thread Tools Rate Thread

DataTableCollection object question

 
 
rodchar
Guest
Posts: n/a
 
      29th Nov 2007
hey all,

this is what i thought would make sense to me but got the infamous "Object
not instantiated" error:

DataTable dt;
dt = mySprocDt;
DataTableCollection dtColl = null;
dtColl.Add(dt);

instead i had to do this to make it work:


DataSet ds = new DataSet( );
DataTableCollection dtColl = null;
DataTable dt = null;
dt = mySprocDt;
ds.Tables.Add(dt);
dtColl = ds.Tables;

can someone please explain why the first one didn't work?

thanks,
rodchar
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      29th Nov 2007
rodchar,

Well, your dtColl variable is null, and you tried to call a method on
it. You have to assign to the variable if you are going to call a method on
it.

Also, the first section of code shouldn't compile, as you should need to
have assigned something to dt in the declaration. I assumed you had
something like:

DataTable dt = null;

Or:

DataTable dt = mySprocDt;

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)


"rodchar" <(E-Mail Removed)> wrote in message
news:6EC4230B-C706-4552-8E7D-(E-Mail Removed)...
> hey all,
>
> this is what i thought would make sense to me but got the infamous "Object
> not instantiated" error:
>
> DataTable dt;
> dt = mySprocDt;
> DataTableCollection dtColl = null;
> dtColl.Add(dt);
>
> instead i had to do this to make it work:
>
>
> DataSet ds = new DataSet( );
> DataTableCollection dtColl = null;
> DataTable dt = null;
> dt = mySprocDt;
> ds.Tables.Add(dt);
> dtColl = ds.Tables;
>
> can someone please explain why the first one didn't work?
>
> thanks,
> rodchar



 
Reply With Quote
 
rodchar
Guest
Posts: n/a
 
      29th Nov 2007
i tried:
DataTableCollection dtColl = new DataTableCollection( )
but that didn't work, how would i instantiate it?

"Nicholas Paldino [.NET/C# MVP]" wrote:

> rodchar,
>
> Well, your dtColl variable is null, and you tried to call a method on
> it. You have to assign to the variable if you are going to call a method on
> it.
>
> Also, the first section of code shouldn't compile, as you should need to
> have assigned something to dt in the declaration. I assumed you had
> something like:
>
> DataTable dt = null;
>
> Or:
>
> DataTable dt = mySprocDt;
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
>
> "rodchar" <(E-Mail Removed)> wrote in message
> news:6EC4230B-C706-4552-8E7D-(E-Mail Removed)...
> > hey all,
> >
> > this is what i thought would make sense to me but got the infamous "Object
> > not instantiated" error:
> >
> > DataTable dt;
> > dt = mySprocDt;
> > DataTableCollection dtColl = null;
> > dtColl.Add(dt);
> >
> > instead i had to do this to make it work:
> >
> >
> > DataSet ds = new DataSet( );
> > DataTableCollection dtColl = null;
> > DataTable dt = null;
> > dt = mySprocDt;
> > ds.Tables.Add(dt);
> > dtColl = ds.Tables;
> >
> > can someone please explain why the first one didn't work?
> >
> > thanks,
> > rodchar

>
>
>

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      30th Nov 2007
Well, what are you trying to create this list for? If you just want a
list of DataTable instances, then why not use List<DataTable>? The
DataTableCollection is tied to the DataSet and it's constructor is
internal/private to System.Data, so you won't be able to instantiate it
directly.

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"rodchar" <(E-Mail Removed)> wrote in message
news643AB46-7786-4002-B0CB-(E-Mail Removed)...
>i tried:
> DataTableCollection dtColl = new DataTableCollection( )
> but that didn't work, how would i instantiate it?
>
> "Nicholas Paldino [.NET/C# MVP]" wrote:
>
>> rodchar,
>>
>> Well, your dtColl variable is null, and you tried to call a method on
>> it. You have to assign to the variable if you are going to call a method
>> on
>> it.
>>
>> Also, the first section of code shouldn't compile, as you should need
>> to
>> have assigned something to dt in the declaration. I assumed you had
>> something like:
>>
>> DataTable dt = null;
>>
>> Or:
>>
>> DataTable dt = mySprocDt;
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - (E-Mail Removed)
>>
>>
>> "rodchar" <(E-Mail Removed)> wrote in message
>> news:6EC4230B-C706-4552-8E7D-(E-Mail Removed)...
>> > hey all,
>> >
>> > this is what i thought would make sense to me but got the infamous
>> > "Object
>> > not instantiated" error:
>> >
>> > DataTable dt;
>> > dt = mySprocDt;
>> > DataTableCollection dtColl = null;
>> > dtColl.Add(dt);
>> >
>> > instead i had to do this to make it work:
>> >
>> >
>> > DataSet ds = new DataSet( );
>> > DataTableCollection dtColl = null;
>> > DataTable dt = null;
>> > dt = mySprocDt;
>> > ds.Tables.Add(dt);
>> > dtColl = ds.Tables;
>> >
>> > can someone please explain why the first one didn't work?
>> >
>> > thanks,
>> > rodchar

>>
>>
>>



 
Reply With Quote
 
rodchar
Guest
Posts: n/a
 
      4th Dec 2007
Thanks Nicholas this last reply gave me insight. i didn't know that the
DataTableCollection was tied to the DataSet. I also haven't worked with
generics before so I don't know what List<DataTable> but I'll definitely try
to do some reading up it.
thanks again,
rod.

"Nicholas Paldino [.NET/C# MVP]" wrote:

> Well, what are you trying to create this list for? If you just want a
> list of DataTable instances, then why not use List<DataTable>? The
> DataTableCollection is tied to the DataSet and it's constructor is
> internal/private to System.Data, so you won't be able to instantiate it
> directly.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "rodchar" <(E-Mail Removed)> wrote in message
> news643AB46-7786-4002-B0CB-(E-Mail Removed)...
> >i tried:
> > DataTableCollection dtColl = new DataTableCollection( )
> > but that didn't work, how would i instantiate it?
> >
> > "Nicholas Paldino [.NET/C# MVP]" wrote:
> >
> >> rodchar,
> >>
> >> Well, your dtColl variable is null, and you tried to call a method on
> >> it. You have to assign to the variable if you are going to call a method
> >> on
> >> it.
> >>
> >> Also, the first section of code shouldn't compile, as you should need
> >> to
> >> have assigned something to dt in the declaration. I assumed you had
> >> something like:
> >>
> >> DataTable dt = null;
> >>
> >> Or:
> >>
> >> DataTable dt = mySprocDt;
> >>
> >> --
> >> - Nicholas Paldino [.NET/C# MVP]
> >> - (E-Mail Removed)
> >>
> >>
> >> "rodchar" <(E-Mail Removed)> wrote in message
> >> news:6EC4230B-C706-4552-8E7D-(E-Mail Removed)...
> >> > hey all,
> >> >
> >> > this is what i thought would make sense to me but got the infamous
> >> > "Object
> >> > not instantiated" error:
> >> >
> >> > DataTable dt;
> >> > dt = mySprocDt;
> >> > DataTableCollection dtColl = null;
> >> > dtColl.Add(dt);
> >> >
> >> > instead i had to do this to make it work:
> >> >
> >> >
> >> > DataSet ds = new DataSet( );
> >> > DataTableCollection dtColl = null;
> >> > DataTable dt = null;
> >> > dt = mySprocDt;
> >> > ds.Tables.Add(dt);
> >> > dtColl = ds.Tables;
> >> >
> >> > can someone please explain why the first one didn't work?
> >> >
> >> > thanks,
> >> > rodchar
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
datatablecollection and missing schema rodchar Microsoft C# .NET 3 26th Mar 2008 09:15 PM
Question regarding initializing base class object to derived class object, archana Microsoft C# .NET 1 9th Jan 2007 07:50 AM
Question about a System.Threading.Timer object and the "state" object you pass to it... Paul Tomlinson Microsoft C# .NET 1 13th May 2005 01:11 PM
Memory question when returning an object from inside another object Steven Blair Microsoft C# .NET 10 11th Jan 2005 07:15 PM
Question: Object Reference not set to an instance of an object. =?Utf-8?B?TWFubnkgQ2hvaGFu?= Microsoft ASP .NET 4 3rd Jan 2005 09:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:21 PM.