How to put database results into an array?

Discussion in 'ASP .Net' started by Haydnw, Aug 15, 2004.

  1. Haydnw

    Haydnw Guest

    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
     
    Haydnw, Aug 15, 2004
    #1
    1. Advertising

  2. Haydnw

    Karl Guest

    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[j]);
    }
    }

    Karl

    "Haydnw" <> wrote in message
    news:...
    > 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
    >
    >
     
    Karl, Aug 15, 2004
    #2
    1. Advertising

  3. Haydnw

    Haydnw Guest

    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:...
    > 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[j]);
    > }
    > }
    >
    > Karl
    >
    > "Haydnw" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
     
    Haydnw, Aug 16, 2004
    #3
  4. Haydnw

    Karl Guest

    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" <> wrote in message
    news:%...
    > 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:...
    > > 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[j]);
    > > }
    > > }
    > >
    > > Karl
    > >
    > > "Haydnw" <> wrote in message
    > > news:...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    Karl, Aug 16, 2004
    #4
  5. Haydnw

    Haydnw Guest

    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:%...
    > 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" <> wrote in message
    > news:%...
    > > 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:...
    > > > 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[j]);
    > > > }
    > > > }
    > > >
    > > > Karl
    > > >
    > > > "Haydnw" <> wrote in message
    > > > news:...
    > > > > 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
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Haydnw, Aug 16, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    2
    Views:
    3,666
    Steve C. Orr [MVP, MCSD]
    Oct 4, 2004
  2. Replies:
    8
    Views:
    465
  3. Replies:
    1
    Views:
    519
    steve
    Dec 24, 2004
  4. Henry Jones

    Put results of a Function into HTML code

    Henry Jones, Jul 27, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    318
    Henry Jones
    Jul 27, 2006
  5. loyed.preetham
    Replies:
    0
    Views:
    450
    loyed.preetham
    Oct 10, 2008
Loading...

Share This Page