423,309 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,309 IT Pros & Developers. It's quick & easy.

code sample wanted: creating and populating a collection of objects from a datasource?

P: n/a
Trying to learn about manipulating collections of objects, and populating
these objects dynamically from datasources. Could someone post a code sample
that shows the following:

Instantiating a collection object -- say, a dictionary.
Populating that collection object with custom objects, say, Person. What I
really want to see is how to populate the properties of those Person objects
from a datasource: instantiate one Person, fill Person.FirstName,
Person.LastName, Person.ID with data, iterate to the next item in the
datasource, instantiate another person, fill Person.FirstName,
Person.LastName...

Any help out there?

Thanks very much.

-KF
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
This BarGraph web control contains a collection of Bar objects.
Full source code is included:
http://www.SteveOrr.net/articles/BarGraphs.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
<ke*****@nospam.nospam> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
Trying to learn about manipulating collections of objects, and populating
these objects dynamically from datasources. Could someone post a code
sample that shows the following:

Instantiating a collection object -- say, a dictionary.
Populating that collection object with custom objects, say, Person. What I
really want to see is how to populate the properties of those Person
objects from a datasource: instantiate one Person, fill Person.FirstName,
Person.LastName, Person.ID with data, iterate to the next item in the
datasource, instantiate another person, fill Person.FirstName,
Person.LastName...

Any help out there?

Thanks very much.

-KF

Nov 19 '05 #2

P: n/a
Thank you very much, Steve, that's quite helpful.

Does anyone have additional examples, or a C# example that I could look at?

Thanks
-KF
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:O0*************@TK2MSFTNGP09.phx.gbl...
This BarGraph web control contains a collection of Bar objects.
Full source code is included:
http://www.SteveOrr.net/articles/BarGraphs.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
<ke*****@nospam.nospam> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
Trying to learn about manipulating collections of objects, and populating
these objects dynamically from datasources. Could someone post a code
sample that shows the following:

Instantiating a collection object -- say, a dictionary.
Populating that collection object with custom objects, say, Person. What
I really want to see is how to populate the properties of those Person
objects from a datasource: instantiate one Person, fill Person.FirstName,
Person.LastName, Person.ID with data, iterate to the next item in the
datasource, instantiate another person, fill Person.FirstName,
Person.LastName...

Any help out there?

Thanks very much.

-KF


Nov 19 '05 #3

P: n/a
Hi KF,

Since the question you mentioned is a very specific one, instead of
searching some separate articles, I've made a simple test page which build
a dummy datatable and populate a "person" class's instance collection from
the datatable and bind it to a datagrid for displaying, below is the
completet code. Also, I think you can try have a look in the www. asp.net
or codeproject site to see whether there are any good examples or articles
there.

Hope helps,

========aspx============
<HTML>
<HEAD>
<title>objectPopulate</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
</body>
</HTML>
========code behind============
namespace DemoWebApp
{
public class objectPopulate : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DataGrid1.DataSource = GetPersons();
DataGrid1.DataBind();
}
}
private DataTable GetDataSource()
{
DataTable dt = new DataTable();

dt.Columns.Add("id",typeof(long));
dt.Columns.Add("name",typeof(string));
dt.Columns.Add("age",typeof(int));
dt.Columns.Add("email",typeof(string));

for(int i=0;i<10;i++)
{
DataRow dr = dt.NewRow();
dr[0] = i+1;
dr[1] = "Name_" + dr[0];
dr[2] = 25 + i;
dr[3] = dr[1] + "@test.org";

dt.Rows.Add(dr);
}

return dt;
}

private Person[] GetPersons()
{
DataTable dt = GetDataSource();

Person[] persons = new Person[dt.Rows.Count];

for(int i=0;i<persons.Length;i++)
{
Person person = new Person();
person.Id = (long)dt.Rows[i][0];
person.Name = (string)dt.Rows[i][1];
person.Age = (int)dt.Rows[i][2];
person.Email = (string)dt.Rows[i][3];

persons[i] = person;
}

return persons;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
public class Person
{
long _id;
string _name;
int _age;
string _email;

public long Id
{
get
{
return _id;
}
set
{
_id = value;
}
}

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}

public string Email
{
get
{
return _email;
}
set
{
_email = value;
}
}
public Person():this(0,"",20,"")
{
}

public Person(long id, string name, int age, string email)
{
_id = id;
_name = name;
_age = age;
_email = email;
}
}
}

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Reply-To: "ke*****@nospam.nospam" <ke*****@u.washington.edu>
| From: <ke*****@nospam.nospam>
| References: <uH**************@tk2msftngp13.phx.gbl>
<O0*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: code sample wanted: creating and populating a collection of
objects from a datasource?
| Date: Wed, 17 Aug 2005 15:17:19 -0700
| Lines: 44
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Response
| Message-ID: <#m**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: idea.urel.washington.edu 128.95.9.12
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118752
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thank you very much, Steve, that's quite helpful.
|
| Does anyone have additional examples, or a C# example that I could look
at?
|
| Thanks
| -KF
|
|
| "Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
| news:O0*************@TK2MSFTNGP09.phx.gbl...
| > This BarGraph web control contains a collection of Bar objects.
| > Full source code is included:
| > http://www.SteveOrr.net/articles/BarGraphs.aspx
| >
| > --
| > I hope this helps,
| > Steve C. Orr, MCSD, MVP
| > http://SteveOrr.net
| >
| >
| > <ke*****@nospam.nospam> wrote in message
| > news:uH**************@tk2msftngp13.phx.gbl...
| >> Trying to learn about manipulating collections of objects, and
populating
| >> these objects dynamically from datasources. Could someone post a code
| >> sample that shows the following:
| >>
| >> Instantiating a collection object -- say, a dictionary.
| >> Populating that collection object with custom objects, say, Person.
What
| >> I really want to see is how to populate the properties of those Person
| >> objects from a datasource: instantiate one Person, fill
Person.FirstName,
| >> Person.LastName, Person.ID with data, iterate to the next item in the
| >> datasource, instantiate another person, fill Person.FirstName,
| >> Person.LastName...
| >>
| >> Any help out there?
| >>
| >> Thanks very much.
| >>
| >> -KF
| >>
| >
| >
|
|
|

Nov 19 '05 #4

P: n/a
Super. Awesome. This is great. Thank you so much, Steven.

Examples of this sort are surprisingly scarce: most online examples seem
work directly against the data objects rather than dyamically populating
custom objects as you've done here.

Thanks again.
-KF

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:5l**************@TK2MSFTNGXA01.phx.gbl...
Hi KF,

Since the question you mentioned is a very specific one, instead of
searching some separate articles, I've made a simple test page which build
a dummy datatable and populate a "person" class's instance collection from
the datatable and bind it to a datagrid for displaying, below is the
completet code. Also, I think you can try have a look in the www. asp.net
or codeproject site to see whether there are any good examples or articles
there.

Hope helps,

========aspx============
<HTML>
<HEAD>
<title>objectPopulate</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
</body>
</HTML>
========code behind============
namespace DemoWebApp
{
public class objectPopulate : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DataGrid1.DataSource = GetPersons();
DataGrid1.DataBind();
}
}
private DataTable GetDataSource()
{
DataTable dt = new DataTable();

dt.Columns.Add("id",typeof(long));
dt.Columns.Add("name",typeof(string));
dt.Columns.Add("age",typeof(int));
dt.Columns.Add("email",typeof(string));

for(int i=0;i<10;i++)
{
DataRow dr = dt.NewRow();
dr[0] = i+1;
dr[1] = "Name_" + dr[0];
dr[2] = 25 + i;
dr[3] = dr[1] + "@test.org";

dt.Rows.Add(dr);
}

return dt;
}

private Person[] GetPersons()
{
DataTable dt = GetDataSource();

Person[] persons = new Person[dt.Rows.Count];

for(int i=0;i<persons.Length;i++)
{
Person person = new Person();
person.Id = (long)dt.Rows[i][0];
person.Name = (string)dt.Rows[i][1];
person.Age = (int)dt.Rows[i][2];
person.Email = (string)dt.Rows[i][3];

persons[i] = person;
}

return persons;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
public class Person
{
long _id;
string _name;
int _age;
string _email;

public long Id
{
get
{
return _id;
}
set
{
_id = value;
}
}

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}

public string Email
{
get
{
return _email;
}
set
{
_email = value;
}
}
public Person():this(0,"",20,"")
{
}

public Person(long id, string name, int age, string email)
{
_id = id;
_name = name;
_age = age;
_email = email;
}
}
}

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Reply-To: "ke*****@nospam.nospam" <ke*****@u.washington.edu>
| From: <ke*****@nospam.nospam>
| References: <uH**************@tk2msftngp13.phx.gbl>
<O0*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: code sample wanted: creating and populating a collection of
objects from a datasource?
| Date: Wed, 17 Aug 2005 15:17:19 -0700
| Lines: 44
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Response
| Message-ID: <#m**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: idea.urel.washington.edu 128.95.9.12
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118752
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thank you very much, Steve, that's quite helpful.
|
| Does anyone have additional examples, or a C# example that I could look
at?
|
| Thanks
| -KF
|
|
| "Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
| news:O0*************@TK2MSFTNGP09.phx.gbl...
| > This BarGraph web control contains a collection of Bar objects.
| > Full source code is included:
| > http://www.SteveOrr.net/articles/BarGraphs.aspx
| >
| > --
| > I hope this helps,
| > Steve C. Orr, MCSD, MVP
| > http://SteveOrr.net
| >
| >
| > <ke*****@nospam.nospam> wrote in message
| > news:uH**************@tk2msftngp13.phx.gbl...
| >> Trying to learn about manipulating collections of objects, and
populating
| >> these objects dynamically from datasources. Could someone post a code
| >> sample that shows the following:
| >>
| >> Instantiating a collection object -- say, a dictionary.
| >> Populating that collection object with custom objects, say, Person.
What
| >> I really want to see is how to populate the properties of those
Person
| >> objects from a datasource: instantiate one Person, fill
Person.FirstName,
| >> Person.LastName, Person.ID with data, iterate to the next item in the
| >> datasource, instantiate another person, fill Person.FirstName,
| >> Person.LastName...
| >>
| >> Any help out there?
| >>
| >> Thanks very much.
| >>
| >> -KF
| >>
| >
| >
|
|
|

Nov 19 '05 #5

P: n/a
You're welcome KF,

Glad that it is of assistance. :-)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Reply-To: "ke*****@nospam.nospam" <ke*****@u.washington.edu>
| From: <ke*****@nospam.nospam>
| References: <uH**************@tk2msftngp13.phx.gbl>
<O0*************@TK2MSFTNGP09.phx.gbl>
<#m**************@tk2msftngp13.phx.gbl>
<5l**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: code sample wanted: creating and populating a collection of
objects from a datasource?
| Date: Wed, 17 Aug 2005 20:45:54 -0700
| Lines: 268
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <O2**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: idea.urel.washington.edu 128.95.9.12
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118790
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Super. Awesome. This is great. Thank you so much, Steven.
|
| Examples of this sort are surprisingly scarce: most online examples seem
| work directly against the data objects rather than dyamically populating
| custom objects as you've done here.
|
| Thanks again.
| -KF
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:5l**************@TK2MSFTNGXA01.phx.gbl...
| > Hi KF,
| >
| > Since the question you mentioned is a very specific one, instead of
| > searching some separate articles, I've made a simple test page which
build
| > a dummy datatable and populate a "person" class's instance collection
from
| > the datatable and bind it to a datagrid for displaying, below is the
| > completet code. Also, I think you can try have a look in the www.
asp.net
| > or codeproject site to see whether there are any good examples or
articles
| > there.
| >
| > Hope helps,
| >
| > ========aspx============
| > <HTML>
| > <HEAD>
| > <title>objectPopulate</title>
| > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
| > <meta name="CODE_LANGUAGE" Content="C#">
| > <meta name="vs_defaultClientScript" content="JavaScript">
| > <meta name="vs_targetSchema"
| > content="http://schemas.microsoft.com/intellisense/ie5">
| > </HEAD>
| > <body>
| > <form id="Form1" method="post" runat="server">
| > <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
| > </form>
| > </body>
| > </HTML>
| >
| >
| > ========code behind============
| > namespace DemoWebApp
| > {
| > public class objectPopulate : System.Web.UI.Page
| > {
| > protected System.Web.UI.WebControls.DataGrid DataGrid1;
| >
| > private void Page_Load(object sender, System.EventArgs e)
| > {
| > if(!IsPostBack)
| > {
| > DataGrid1.DataSource = GetPersons();
| > DataGrid1.DataBind();
| > }
| > }
| >
| >
| > private DataTable GetDataSource()
| > {
| > DataTable dt = new DataTable();
| >
| > dt.Columns.Add("id",typeof(long));
| > dt.Columns.Add("name",typeof(string));
| > dt.Columns.Add("age",typeof(int));
| > dt.Columns.Add("email",typeof(string));
| >
| > for(int i=0;i<10;i++)
| > {
| > DataRow dr = dt.NewRow();
| > dr[0] = i+1;
| > dr[1] = "Name_" + dr[0];
| > dr[2] = 25 + i;
| > dr[3] = dr[1] + "@test.org";
| >
| > dt.Rows.Add(dr);
| > }
| >
| > return dt;
| > }
| >
| > private Person[] GetPersons()
| > {
| > DataTable dt = GetDataSource();
| >
| > Person[] persons = new Person[dt.Rows.Count];
| >
| > for(int i=0;i<persons.Length;i++)
| > {
| > Person person = new Person();
| > person.Id = (long)dt.Rows[i][0];
| > person.Name = (string)dt.Rows[i][1];
| > person.Age = (int)dt.Rows[i][2];
| > person.Email = (string)dt.Rows[i][3];
| >
| > persons[i] = person;
| > }
| >
| > return persons;
| > }
| >
| > #region Web Form Designer generated code
| > override protected void OnInit(EventArgs e)
| > {
| > InitializeComponent();
| > base.OnInit(e);
| > }
| >
| > private void InitializeComponent()
| > {
| > this.Load += new System.EventHandler(this.Page_Load);
| >
| > }
| > #endregion
| > }
| >
| >
| > public class Person
| > {
| > long _id;
| > string _name;
| > int _age;
| > string _email;
| >
| > public long Id
| > {
| > get
| > {
| > return _id;
| > }
| > set
| > {
| > _id = value;
| > }
| > }
| >
| > public string Name
| > {
| > get
| > {
| > return _name;
| > }
| > set
| > {
| > _name = value;
| > }
| > }
| >
| > public int Age
| > {
| > get
| > {
| > return _age;
| > }
| > set
| > {
| > _age = value;
| > }
| > }
| >
| > public string Email
| > {
| > get
| > {
| > return _email;
| > }
| > set
| > {
| > _email = value;
| > }
| > }
| >
| >
| > public Person():this(0,"",20,"")
| > {
| > }
| >
| > public Person(long id, string name, int age, string email)
| > {
| > _id = id;
| > _name = name;
| > _age = age;
| > _email = email;
| > }
| > }
| > }
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| > --------------------
| > | Reply-To: "ke*****@nospam.nospam" <ke*****@u.washington.edu>
| > | From: <ke*****@nospam.nospam>
| > | References: <uH**************@tk2msftngp13.phx.gbl>
| > <O0*************@TK2MSFTNGP09.phx.gbl>
| > | Subject: Re: code sample wanted: creating and populating a collection
of
| > objects from a datasource?
| > | Date: Wed, 17 Aug 2005 15:17:19 -0700
| > | Lines: 44
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Response
| > | Message-ID: <#m**************@tk2msftngp13.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: idea.urel.washington.edu 128.95.9.12
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:118752
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Thank you very much, Steve, that's quite helpful.
| > |
| > | Does anyone have additional examples, or a C# example that I could
look
| > at?
| > |
| > | Thanks
| > | -KF
| > |
| > |
| > | "Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
| > | news:O0*************@TK2MSFTNGP09.phx.gbl...
| > | > This BarGraph web control contains a collection of Bar objects.
| > | > Full source code is included:
| > | > http://www.SteveOrr.net/articles/BarGraphs.aspx
| > | >
| > | > --
| > | > I hope this helps,
| > | > Steve C. Orr, MCSD, MVP
| > | > http://SteveOrr.net
| > | >
| > | >
| > | > <ke*****@nospam.nospam> wrote in message
| > | > news:uH**************@tk2msftngp13.phx.gbl...
| > | >> Trying to learn about manipulating collections of objects, and
| > populating
| > | >> these objects dynamically from datasources. Could someone post a
code
| > | >> sample that shows the following:
| > | >>
| > | >> Instantiating a collection object -- say, a dictionary.
| > | >> Populating that collection object with custom objects, say, Person.
| > What
| > | >> I really want to see is how to populate the properties of those
| > Person
| > | >> objects from a datasource: instantiate one Person, fill
| > Person.FirstName,
| > | >> Person.LastName, Person.ID with data, iterate to the next item in
the
| > | >> datasource, instantiate another person, fill Person.FirstName,
| > | >> Person.LastName...
| > | >>
| > | >> Any help out there?
| > | >>
| > | >> Thanks very much.
| > | >>
| > | >> -KF
| > | >>
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.