Home All Groups Group Topic Archive Search About

DataTableCollection object question

Author
29 Nov 2007 9:40 PM
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

Author
29 Nov 2007 10:09 PM
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;

--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com


Show quote
"rodchar" <rodc***@discussions.microsoft.com> wrote in message
news:6EC4230B-C706-4552-8E7D-A3278F3A79AD@microsoft.com...
> 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
Author
29 Nov 2007 10:22 PM
rodchar
i tried:
DataTableCollection dtColl = new DataTableCollection( )
but that didn't work, how would i instantiate it?

Show quote
"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]
>           - mvp@spam.guard.caspershouse.com
>
>
> "rodchar" <rodc***@discussions.microsoft.com> wrote in message
> news:6EC4230B-C706-4552-8E7D-A3278F3A79AD@microsoft.com...
> > 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
>
>
>

AddThis Social Bookmark Button