DataTableCollection object question

R

rodchar

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
 
N

Nicholas Paldino [.NET/C# MVP]

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;
 
R

rodchar

i tried:
DataTableCollection dtColl = new DataTableCollection( )
but that didn't work, how would i instantiate it?

Nicholas Paldino said:
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 address removed)


rodchar said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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 address removed)

rodchar said:
i tried:
DataTableCollection dtColl = new DataTableCollection( )
but that didn't work, how would i instantiate it?

Nicholas Paldino said:
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 address removed)


rodchar said:
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
 
R

rodchar

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 said:
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 address removed)

rodchar said:
i tried:
DataTableCollection dtColl = new DataTableCollection( )
but that didn't work, how would i instantiate it?

Nicholas Paldino said:
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 address 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top