Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to put database results into an array?

Reply
Thread Tools

How to put database results into an array?

 
 
Haydnw
Guest
Posts: n/a
 
      08-15-2004
Hi,

I'd like to put a load of database results (several rows for 5 fields) into
a two-dimensional array. Now, this may be a really stupid question, but can
someone give me a pointer for how to do it? I can bind data to datagrids and
lists and stuff all day long, but can't seem to grasp this one. Any pointers
to a useful article / demo (or just any useful pointers!) would be much
appreciated.

Thanks,
Haydn


 
Reply With Quote
 
 
 
 
Karl
Guest
Posts: n/a
 
      08-15-2004
Haydn,
I'd love to know why you want to do this there is likely a better
alternative for whatever it is you are trying to do.

The only way I know how to do what you want to do is to manually loop
through your datasource:

DataTable dt = new DataTable();
dt.Columns.Add("column1");
dt.Columns.Add("column2");
dt.Columns.Add("column3");
for (int i = 0; i < 5; i++){
DataRow dr = dt.NewRow();
dr[0] = 1 * (i + 1);
dr[1] = 3 * (i + 1);
dr[2] = 2 * (i + 1);
dt.Rows.Add(dr);
}
int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++){
for (int j = 0; j < dt.Columns.Count; j++){
array[j,i] = Convert.ToInt32(dt.Rows[i][j]);
}
}

Karl

"Haydnw" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I'd like to put a load of database results (several rows for 5 fields)

into
> a two-dimensional array. Now, this may be a really stupid question, but

can
> someone give me a pointer for how to do it? I can bind data to datagrids

and
> lists and stuff all day long, but can't seem to grasp this one. Any

pointers
> to a useful article / demo (or just any useful pointers!) would be much
> appreciated.
>
> Thanks,
> Haydn
>
>



 
Reply With Quote
 
 
 
 
Haydnw
Guest
Posts: n/a
 
      08-16-2004
Hi Karl, thanks for the reply. I'm fairly new to .NET in general, so it's
likely there is a better way! Basically I have a database containing info
about lots of different images, including the filename. The images are
viewed one at a time, on a page with 'Next' and 'Previous' buttons.
Obviously, the 'Next' button takes you to the next image in the database.
However, file names are numeric but not sequential so the current file name
has no relation to whatever comes next! So if I'm viewing an image with
filename 00003, I have no idea what the next one is.

Basically what I need is a way to return a set of results from access, and
say 'Get the SECOND row this time' or 'Get the THIRD row this time'. I don't
know any way of doing this. I thought that by dumping them in an array, I
would be able to reference the values in the array in a more structured
fashion because I know they are sequential starting from 0.

One alternative is to modify my SQL to get the top record where the filename
is greater than the one currently being viewed (if 'Next' button clicked) or
less than the one currently being viewed (if 'Previous' button clicked). I
realised this after my original post and it's now looking favourite! But I
thought another advantage of the array method might be that once I've
queried the database once to get all the info, I can maintain it through
page postbacks rather than querying the database for one record each time
the page is loaded. What are your views on this? Would the overheads of
maintaining a large two-dimensional array be greater than querying the
database each time the page loads?

Thank you very much,
Haydn



"Karl" <none> wrote in message news:(E-Mail Removed)...
> Haydn,
> I'd love to know why you want to do this there is likely a better
> alternative for whatever it is you are trying to do.
>
> The only way I know how to do what you want to do is to manually loop
> through your datasource:
>
> DataTable dt = new DataTable();
> dt.Columns.Add("column1");
> dt.Columns.Add("column2");
> dt.Columns.Add("column3");
> for (int i = 0; i < 5; i++){
> DataRow dr = dt.NewRow();
> dr[0] = 1 * (i + 1);
> dr[1] = 3 * (i + 1);
> dr[2] = 2 * (i + 1);
> dt.Rows.Add(dr);
> }
> int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
> for (int i = 0; i < dt.Rows.Count; i++){
> for (int j = 0; j < dt.Columns.Count; j++){
> array[j,i] = Convert.ToInt32(dt.Rows[i][j]);
> }
> }
>
> Karl
>
> "Haydnw" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi,
> >
> > I'd like to put a load of database results (several rows for 5 fields)

> into
> > a two-dimensional array. Now, this may be a really stupid question, but

> can
> > someone give me a pointer for how to do it? I can bind data to datagrids

> and
> > lists and stuff all day long, but can't seem to grasp this one. Any

> pointers
> > to a useful article / demo (or just any useful pointers!) would be much
> > appreciated.
> >
> > Thanks,
> > Haydn
> >
> >

>
>



 
Reply With Quote
 
Karl
Guest
Posts: n/a
 
      08-16-2004
Without knowing more, I would retrieve all records into a DataTable and
cache the datatable in the HttpRuntime.Cache object. I would use the
querystring to pass the index of the datatable to get. Something like (not
actually code as I'm writting it without my trusty editor, but enough to get
you started)


Page_Load
Dim imageIndex as integer
if Request.QueryString("index") is nothing then
imageIndex = 0
else
imageIndex = Request.Querystring("index")
end if

Dim table as DataTable = GetImages()
if imageIndex >= table.rows.Count then
imageIndex = 0
end if

DataRow dr = table.rows[imageIndex]
'do something with the datarow
End

Function GetImages() as DataTable
Dim cacheKey as string = "AllImages"
DataTable dt = ctype(cache(cacheKey), datatable)
if dt is nothing then
Dim Conn as new SqlConnect("...")
dim cmd as new SqlCommand("command here", conn)
dim da as new DataAdapter(cmd)
dt = new DataTable
da.fill(dt)
'close and dispose your stuff
Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(1),
TimeSpan.Zero)
end if
return dt
End

You can then pass Index=(imageIndex+1) to get the next image and
Index=(imageIndex-1) to get the previous one. Hope that helps.

Karl


"Haydnw" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi Karl, thanks for the reply. I'm fairly new to .NET in general, so it's
> likely there is a better way! Basically I have a database containing info
> about lots of different images, including the filename. The images are
> viewed one at a time, on a page with 'Next' and 'Previous' buttons.
> Obviously, the 'Next' button takes you to the next image in the database.
> However, file names are numeric but not sequential so the current file

name
> has no relation to whatever comes next! So if I'm viewing an image with
> filename 00003, I have no idea what the next one is.
>
> Basically what I need is a way to return a set of results from access, and
> say 'Get the SECOND row this time' or 'Get the THIRD row this time'. I

don't
> know any way of doing this. I thought that by dumping them in an array, I
> would be able to reference the values in the array in a more structured
> fashion because I know they are sequential starting from 0.
>
> One alternative is to modify my SQL to get the top record where the

filename
> is greater than the one currently being viewed (if 'Next' button clicked)

or
> less than the one currently being viewed (if 'Previous' button clicked). I
> realised this after my original post and it's now looking favourite! But I
> thought another advantage of the array method might be that once I've
> queried the database once to get all the info, I can maintain it through
> page postbacks rather than querying the database for one record each time
> the page is loaded. What are your views on this? Would the overheads of
> maintaining a large two-dimensional array be greater than querying the
> database each time the page loads?
>
> Thank you very much,
> Haydn
>
>
>
> "Karl" <none> wrote in message

news:(E-Mail Removed)...
> > Haydn,
> > I'd love to know why you want to do this there is likely a better
> > alternative for whatever it is you are trying to do.
> >
> > The only way I know how to do what you want to do is to manually loop
> > through your datasource:
> >
> > DataTable dt = new DataTable();
> > dt.Columns.Add("column1");
> > dt.Columns.Add("column2");
> > dt.Columns.Add("column3");
> > for (int i = 0; i < 5; i++){
> > DataRow dr = dt.NewRow();
> > dr[0] = 1 * (i + 1);
> > dr[1] = 3 * (i + 1);
> > dr[2] = 2 * (i + 1);
> > dt.Rows.Add(dr);
> > }
> > int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
> > for (int i = 0; i < dt.Rows.Count; i++){
> > for (int j = 0; j < dt.Columns.Count; j++){
> > array[j,i] = Convert.ToInt32(dt.Rows[i][j]);
> > }
> > }
> >
> > Karl
> >
> > "Haydnw" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Hi,
> > >
> > > I'd like to put a load of database results (several rows for 5 fields)

> > into
> > > a two-dimensional array. Now, this may be a really stupid question,

but
> > can
> > > someone give me a pointer for how to do it? I can bind data to

datagrids
> > and
> > > lists and stuff all day long, but can't seem to grasp this one. Any

> > pointers
> > > to a useful article / demo (or just any useful pointers!) would be

much
> > > appreciated.
> > >
> > > Thanks,
> > > Haydn
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Haydnw
Guest
Posts: n/a
 
      08-16-2004
Karl,

Thank you so much! That works very well - excellently in fact! This
technique might prove for a couple of other pages in the site aswell. Thanks
again!

Haydn



"Karl" <none> wrote in message
news:%(E-Mail Removed)...
> Without knowing more, I would retrieve all records into a DataTable and
> cache the datatable in the HttpRuntime.Cache object. I would use the
> querystring to pass the index of the datatable to get. Something like

(not
> actually code as I'm writting it without my trusty editor, but enough to

get
> you started)
>
>
> Page_Load
> Dim imageIndex as integer
> if Request.QueryString("index") is nothing then
> imageIndex = 0
> else
> imageIndex = Request.Querystring("index")
> end if
>
> Dim table as DataTable = GetImages()
> if imageIndex >= table.rows.Count then
> imageIndex = 0
> end if
>
> DataRow dr = table.rows[imageIndex]
> 'do something with the datarow
> End
>
> Function GetImages() as DataTable
> Dim cacheKey as string = "AllImages"
> DataTable dt = ctype(cache(cacheKey), datatable)
> if dt is nothing then
> Dim Conn as new SqlConnect("...")
> dim cmd as new SqlCommand("command here", conn)
> dim da as new DataAdapter(cmd)
> dt = new DataTable
> da.fill(dt)
> 'close and dispose your stuff
> Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(1),
> TimeSpan.Zero)
> end if
> return dt
> End
>
> You can then pass Index=(imageIndex+1) to get the next image and
> Index=(imageIndex-1) to get the previous one. Hope that helps.
>
> Karl
>
>
> "Haydnw" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hi Karl, thanks for the reply. I'm fairly new to .NET in general, so

it's
> > likely there is a better way! Basically I have a database containing

info
> > about lots of different images, including the filename. The images are
> > viewed one at a time, on a page with 'Next' and 'Previous' buttons.
> > Obviously, the 'Next' button takes you to the next image in the

database.
> > However, file names are numeric but not sequential so the current file

> name
> > has no relation to whatever comes next! So if I'm viewing an image with
> > filename 00003, I have no idea what the next one is.
> >
> > Basically what I need is a way to return a set of results from access,

and
> > say 'Get the SECOND row this time' or 'Get the THIRD row this time'. I

> don't
> > know any way of doing this. I thought that by dumping them in an array,

I
> > would be able to reference the values in the array in a more structured
> > fashion because I know they are sequential starting from 0.
> >
> > One alternative is to modify my SQL to get the top record where the

> filename
> > is greater than the one currently being viewed (if 'Next' button

clicked)
> or
> > less than the one currently being viewed (if 'Previous' button clicked).

I
> > realised this after my original post and it's now looking favourite! But

I
> > thought another advantage of the array method might be that once I've
> > queried the database once to get all the info, I can maintain it through
> > page postbacks rather than querying the database for one record each

time
> > the page is loaded. What are your views on this? Would the overheads of
> > maintaining a large two-dimensional array be greater than querying the
> > database each time the page loads?
> >
> > Thank you very much,
> > Haydn
> >
> >
> >
> > "Karl" <none> wrote in message

> news:(E-Mail Removed)...
> > > Haydn,
> > > I'd love to know why you want to do this there is likely a better
> > > alternative for whatever it is you are trying to do.
> > >
> > > The only way I know how to do what you want to do is to manually loop
> > > through your datasource:
> > >
> > > DataTable dt = new DataTable();
> > > dt.Columns.Add("column1");
> > > dt.Columns.Add("column2");
> > > dt.Columns.Add("column3");
> > > for (int i = 0; i < 5; i++){
> > > DataRow dr = dt.NewRow();
> > > dr[0] = 1 * (i + 1);
> > > dr[1] = 3 * (i + 1);
> > > dr[2] = 2 * (i + 1);
> > > dt.Rows.Add(dr);
> > > }
> > > int[,] array = new int[dt.Columns.Count,dt.Rows.Count];
> > > for (int i = 0; i < dt.Rows.Count; i++){
> > > for (int j = 0; j < dt.Columns.Count; j++){
> > > array[j,i] = Convert.ToInt32(dt.Rows[i][j]);
> > > }
> > > }
> > >
> > > Karl
> > >
> > > "Haydnw" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > Hi,
> > > >
> > > > I'd like to put a load of database results (several rows for 5

fields)
> > > into
> > > > a two-dimensional array. Now, this may be a really stupid question,

> but
> > > can
> > > > someone give me a pointer for how to do it? I can bind data to

> datagrids
> > > and
> > > > lists and stuff all day long, but can't seem to grasp this one. Any
> > > pointers
> > > > to a useful article / demo (or just any useful pointers!) would be

> much
> > > > appreciated.
> > > >
> > > > Thanks,
> > > > Haydn
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Put results of a Function into HTML code Henry Jones ASP .Net 2 07-27-2006 09:26 PM
Where do I put my Database relations... at DATABASE level or in PROGRAM LEVEL? mesut@edpnet.be ASP .Net 8 03-17-2006 10:29 AM
Need advice how to write a program to receive an image via email and put it into the SQL Server database Vanessa Lee Wireless Networking 0 02-19-2006 11:45 AM



Advertisments