Reusing DataReader

Discussion in 'ASP .Net' started by Thomas Scheiderich, Apr 11, 2004.

  1. I have 2 dropdowns that are exactly the same and I don't want to re-read
    for each.

    Is there a way to do something like below where I read the data, bind to
    depCity1 and then bind to destCity2. I find that the second one always
    is blank. I assume this is because the DataReader is read-forward only.

    ********************************************************
    connect.open()
    cmdSelect = New OleDbCommand("exec populateCities 'North America'",connect)
    dtReader = cmdSelect.ExecuteReader()

    depCity1.Datasource = dtReader
    depCity1.DatatextField = "city_name"
    depCity1.DataBind()

    destCity2.Datasource = dtReader
    destCity2.DatatextField = "city_name"
    destCity2.DataBind()
    connect.close()
    ********************************************

    If I can't reuse the dtReader I already have, how do I quickly make
    dest2City2 = depCity1?

    Also, I have a stored procedure that doesn't pass back a title. Is
    there a way to do the above where I do something like:

    depCity1.DataTextField = dtReader(0).

    I know the above isn't correct, just trying to find a way to say look at
    column 0.

    Thanks,

    Tom.
     
    Thomas Scheiderich, Apr 11, 2004
    #1
    1. Advertising

  2. Thomas Scheiderich

    Cor Ligthert Guest

    Hi Thomas,

    Almost the same question was in the Adonet newsgroup yesterday.
    The answer where the same, use the databinding, however I made a code sample
    for you how you can do it using the datareader.

    I hope this helps?

    Cor
    \\\
    Dim rdr As SqlDataReader
    rdr = cmd.ExecuteReader()
    While rdr.Read()
    Dim item As New ListItem
    item.Text = rdr.GetInt32(0).ToString 'for an integer value from the
    database
    item.Value = rdr.GetString(1) 'for a string value from the database
    DropDownList1.Items.Add(item)
    DropDownList2.Items.Add(item)
    End While
    ///
    I hope this helps?

    Cor
     
    Cor Ligthert, Apr 11, 2004
    #2
    1. Advertising

  3. Thomas Scheiderich

    coollzh Guest

    i think you can store the data of dataReader into a IList container,then you
    can user the IList anywhere
    "Cor Ligthert" <> дÈëÏûÏ¢
    news:...
    > Hi Thomas,
    >
    > Almost the same question was in the Adonet newsgroup yesterday.
    > The answer where the same, use the databinding, however I made a code

    sample
    > for you how you can do it using the datareader.
    >
    > I hope this helps?
    >
    > Cor
    > \\\
    > Dim rdr As SqlDataReader
    > rdr = cmd.ExecuteReader()
    > While rdr.Read()
    > Dim item As New ListItem
    > item.Text = rdr.GetInt32(0).ToString 'for an integer value from the
    > database
    > item.Value = rdr.GetString(1) 'for a string value from the database
    > DropDownList1.Items.Add(item)
    > DropDownList2.Items.Add(item)
    > End While
    > ///
    > I hope this helps?
    >
    > Cor
    >
    >
     
    coollzh, Apr 11, 2004
    #3
  4. Thomas Scheiderich

    Cor Ligthert Guest

    Hi Coollzh,

    I said this question has been in the adonet group yesterday too, there the
    answer was to use an adapter and a datatable, however this is the shortest
    way to do it in my opinion with the datareader (for which I see no real
    advantage).

    However what is the advantages of using an Ilist solution, I did think of
    giving that, however I saw no advantage.

    Cor
     
    Cor Ligthert, Apr 11, 2004
    #4
  5. Thomas Scheiderich

    coollzh Guest

    Actually , in most conditions the IList filled with DataReader is more
    effecitive solution.
    IList is a interface, you can bind the object that implement IList interface
    to a datagrid or a Repeator.

    "Cor Ligthert" <> дÈëÏûÏ¢
    news:#...
    > Hi Coollzh,
    >
    > I said this question has been in the adonet group yesterday too, there the
    > answer was to use an adapter and a datatable, however this is the shortest
    > way to do it in my opinion with the datareader (for which I see no real
    > advantage).
    >
    > However what is the advantages of using an Ilist solution, I did think of
    > giving that, however I saw no advantage.
    >
    > Cor
    >
    >
     
    coollzh, Apr 11, 2004
    #5
  6. coollzh wrote:

    > Actually , in most conditions the IList filled with DataReader is more
    > effecitive solution.
    > IList is a interface, you can bind the object that implement IList interface


    > to a datagrid or a Repeator.



    Do you have an example on how you can do that? I am trying to play with
    all the options to learn the pluses and minuses of each technique.

    I was looking at other ways other than looping through and adding each
    field one by one.

    The way I originally showed was good for only binding to one object (I
    just had need of using the same data in a couple of places and couldn't
    figure out why I couldn't just bind to another object. Later I found
    out why - read forward only with DataReader).

    I then thought there might be a way to do some type of doing an
    assignment without looping through the object (object1.values =
    object2.values).

    I read that the DataReader was the fastest and most efficient method for
    reading, although not as robust as the DataSet (adapter). For the Web,
    according to what I read (and I may be wrong here), you want speed and
    the use of DataReader with Stored Procedures would provide the maximum
    data access speed.

    But speed is not the only factor to consider, in my case - using
    DataReader, I would have to re-read multiple times to get the same
    information to each object (or loop through assigning to each object
    together as was demonstrated by Cor).

    This then gives me another problem. What if I am passed back multiple
    fields in my DataReader (name, address, city, state and zip, for
    example) and want to do the databinding I did in my first post - I
    assume this would not be possible. For example:

    ********************************************************
    connect.open()
    cmdSelect = New OleDbCommand("exec getCustomers",connect)
    dtReader = cmdSelect.ExecuteReader()

    Name.Datasource = dtReader
    Name.DatatextField = "name"
    Name.DataBind()

    Address.Datasource = dtReader
    Address.DatatextField = "address"
    Address.DataBind()

    City.Datasource = dtReader
    City.DatatextField = "city"
    City.DataBind()

    State.Datasource = dtReader
    State.DatatextField = "state"
    State.DataBind()

    Zip.Datasource = dtReader
    Zip.DatatextField = "zip"
    Zip.DataBind()
    connect.close()
    ********************************************

    If this is the case, would this work where I just use a Page DataBind:


    ********************************************************
    connect.open()
    cmdSelect = New OleDbCommand("exec getCustomers",connect)
    dtReader = cmdSelect.ExecuteReader()

    Name.Datasource = dtReader
    Name.DatatextField = "name"

    Address.Datasource = dtReader
    Address.DatatextField = "address"

    City.Datasource = dtReader
    City.DatatextField = "city"

    State.Datasource = dtReader
    State.DatatextField = "state"

    Zip.Datasource = dtReader
    Zip.DatatextField = "zip"

    DataBind()
    connect.close()
    ********************************************

    Also, what was the title of the posts that dealt with this yesterday

    Thanks,

    Tom.

    >
    > "Cor Ligthert" <> дÈëÏûÏ¢
    > news:#...
    >
    >>Hi Coollzh,
    >>
    >>I said this question has been in the adonet group yesterday too, there the
    >>answer was to use an adapter and a datatable, however this is the shortest
    >>way to do it in my opinion with the datareader (for which I see no real
    >>advantage).
    >>
    >>However what is the advantages of using an Ilist solution, I did think of
    >>giving that, however I saw no advantage.
    >>
    >>Cor
    >>
    >>
    >>

    >
    >
     
    Thomas Scheiderich, Apr 11, 2004
    #6
  7. Hello Thomas,

    It seems that your latter approach would not work.
    Databinding can not be performed simultaneously, I guess.

    Since the databinding of the IDataReader, will Read() all
    records from the forward only reader, thus, one control,
    for example, the Name control, finishes the databinding,
    the second control, for example, the Address control,
    would not have any record to Read() any more.

    W. Jordan


    "Thomas Scheiderich" <> wrote in message
    news:...
    > coollzh wrote:
    >
    > ********************************************************
    > connect.open()
    > cmdSelect = New OleDbCommand("exec getCustomers",connect)
    > dtReader = cmdSelect.ExecuteReader()
    >
    > Name.Datasource = dtReader
    > Name.DatatextField = "name"
    > Name.DataBind()
    >
    > Address.Datasource = dtReader
    > Address.DatatextField = "address"
    > Address.DataBind()
    >
    > City.Datasource = dtReader
    > City.DatatextField = "city"
    > City.DataBind()
    >
    > State.Datasource = dtReader
    > State.DatatextField = "state"
    > State.DataBind()
    >
    > Zip.Datasource = dtReader
    > Zip.DatatextField = "zip"
    > Zip.DataBind()
    > connect.close()
    > ********************************************
    >
    > If this is the case, would this work where I just use a Page DataBind:
    >
    >
    > ********************************************************
    > connect.open()
    > cmdSelect = New OleDbCommand("exec getCustomers",connect)
    > dtReader = cmdSelect.ExecuteReader()
    >
    > Name.Datasource = dtReader
    > Name.DatatextField = "name"
    >
    > Address.Datasource = dtReader
    > Address.DatatextField = "address"
    >
    > City.Datasource = dtReader
    > City.DatatextField = "city"
    >
    > State.Datasource = dtReader
    > State.DatatextField = "state"
    >
    > Zip.Datasource = dtReader
    > Zip.DatatextField = "zip"
    >
    > DataBind()
    > connect.close()
    > ********************************************
    >
    > Also, what was the title of the posts that dealt with this yesterday
    >
    > Thanks,
    >
    > Tom.
    >
    > >
    > > "Cor Ligthert" <> дÈëÏûÏ¢
    > > news:#...
    > >
    > >>Hi Coollzh,
    > >>
    > >>I said this question has been in the adonet group yesterday too, there

    the
    > >>answer was to use an adapter and a datatable, however this is the

    shortest
    > >>way to do it in my opinion with the datareader (for which I see no real
    > >>advantage).
    > >>
    > >>However what is the advantages of using an Ilist solution, I did think

    of
    > >>giving that, however I saw no advantage.
    > >>
    > >>Cor
    > >>
    > >>
    > >>

    > >
    > >

    >
     
    Welman Jordan, Apr 12, 2004
    #7
  8. Thomas Scheiderich

    Joe Fallon Guest

    You might want to look at using Business Objects to handle these
    requirements.

    The Datareader can be used to load the BO class and then you can make the BO
    the DataSource for as many controls as you want.

    Rocky Lhotka has built a whole framework you can use called CSLA.
    See: http://www.lhotka.net/ArticleIndex.aspx?area=CSLA .NET
    --
    Joe Fallon




    "Thomas Scheiderich" <> wrote in message
    news:...
    > coollzh wrote:
    >
    > > Actually , in most conditions the IList filled with DataReader is more
    > > effecitive solution.
    > > IList is a interface, you can bind the object that implement IList

    interface
    >
    > > to a datagrid or a Repeator.

    >
    >
    > Do you have an example on how you can do that? I am trying to play with
    > all the options to learn the pluses and minuses of each technique.
    >
    > I was looking at other ways other than looping through and adding each
    > field one by one.
    >
    > The way I originally showed was good for only binding to one object (I
    > just had need of using the same data in a couple of places and couldn't
    > figure out why I couldn't just bind to another object. Later I found
    > out why - read forward only with DataReader).
    >
    > I then thought there might be a way to do some type of doing an
    > assignment without looping through the object (object1.values =
    > object2.values).
    >
    > I read that the DataReader was the fastest and most efficient method for
    > reading, although not as robust as the DataSet (adapter). For the Web,
    > according to what I read (and I may be wrong here), you want speed and
    > the use of DataReader with Stored Procedures would provide the maximum
    > data access speed.
    >
    > But speed is not the only factor to consider, in my case - using
    > DataReader, I would have to re-read multiple times to get the same
    > information to each object (or loop through assigning to each object
    > together as was demonstrated by Cor).
    >
    > This then gives me another problem. What if I am passed back multiple
    > fields in my DataReader (name, address, city, state and zip, for
    > example) and want to do the databinding I did in my first post - I
    > assume this would not be possible. For example:
    >
    > ********************************************************
    > connect.open()
    > cmdSelect = New OleDbCommand("exec getCustomers",connect)
    > dtReader = cmdSelect.ExecuteReader()
    >
    > Name.Datasource = dtReader
    > Name.DatatextField = "name"
    > Name.DataBind()
    >
    > Address.Datasource = dtReader
    > Address.DatatextField = "address"
    > Address.DataBind()
    >
    > City.Datasource = dtReader
    > City.DatatextField = "city"
    > City.DataBind()
    >
    > State.Datasource = dtReader
    > State.DatatextField = "state"
    > State.DataBind()
    >
    > Zip.Datasource = dtReader
    > Zip.DatatextField = "zip"
    > Zip.DataBind()
    > connect.close()
    > ********************************************
    >
    > If this is the case, would this work where I just use a Page DataBind:
    >
    >
    > ********************************************************
    > connect.open()
    > cmdSelect = New OleDbCommand("exec getCustomers",connect)
    > dtReader = cmdSelect.ExecuteReader()
    >
    > Name.Datasource = dtReader
    > Name.DatatextField = "name"
    >
    > Address.Datasource = dtReader
    > Address.DatatextField = "address"
    >
    > City.Datasource = dtReader
    > City.DatatextField = "city"
    >
    > State.Datasource = dtReader
    > State.DatatextField = "state"
    >
    > Zip.Datasource = dtReader
    > Zip.DatatextField = "zip"
    >
    > DataBind()
    > connect.close()
    > ********************************************
    >
    > Also, what was the title of the posts that dealt with this yesterday
    >
    > Thanks,
    >
    > Tom.
    >
    > >
    > > "Cor Ligthert" <> дÈëÏûÏ¢
    > > news:#...
    > >
    > >>Hi Coollzh,
    > >>
    > >>I said this question has been in the adonet group yesterday too, there

    the
    > >>answer was to use an adapter and a datatable, however this is the

    shortest
    > >>way to do it in my opinion with the datareader (for which I see no real
    > >>advantage).
    > >>
    > >>However what is the advantages of using an Ilist solution, I did think

    of
    > >>giving that, however I saw no advantage.
    > >>
    > >>Cor
    > >>
    > >>
    > >>

    > >
    > >

    >
     
    Joe Fallon, Apr 12, 2004
    #8
  9. Thomas Scheiderich

    coollzh Guest

    you can refrence petshop2.0, or petshop3.0 for IList using

    "Thomas Scheiderich" <>
    ??????:...
    > coollzh wrote:
    >
    > > Actually , in most conditions the IList filled with DataReader is more
    > > effecitive solution.
    > > IList is a interface, you can bind the object that implement IList

    interface
    >
    > > to a datagrid or a Repeator.

    >
    >
    > Do you have an example on how you can do that? I am trying to play with
    > all the options to learn the pluses and minuses of each technique.
    >
    > I was looking at other ways other than looping through and adding each
    > field one by one.
    >
    > The way I originally showed was good for only binding to one object (I
    > just had need of using the same data in a couple of places and couldn't
    > figure out why I couldn't just bind to another object. Later I found
    > out why - read forward only with DataReader).
    >
    > I then thought there might be a way to do some type of doing an
    > assignment without looping through the object (object1.values =
    > object2.values).
    >
    > I read that the DataReader was the fastest and most efficient method for
    > reading, although not as robust as the DataSet (adapter). For the Web,
    > according to what I read (and I may be wrong here), you want speed and
    > the use of DataReader with Stored Procedures would provide the maximum
    > data access speed.
    >
    > But speed is not the only factor to consider, in my case - using
    > DataReader, I would have to re-read multiple times to get the same
    > information to each object (or loop through assigning to each object
    > together as was demonstrated by Cor).
    >
    > This then gives me another problem. What if I am passed back multiple
    > fields in my DataReader (name, address, city, state and zip, for
    > example) and want to do the databinding I did in my first post - I
    > assume this would not be possible. For example:
    >
    > ********************************************************
    > connect.open()
    > cmdSelect = New OleDbCommand("exec getCustomers",connect)
    > dtReader = cmdSelect.ExecuteReader()
    >
    > Name.Datasource = dtReader
    > Name.DatatextField = "name"
    > Name.DataBind()
    >
    > Address.Datasource = dtReader
    > Address.DatatextField = "address"
    > Address.DataBind()
    >
    > City.Datasource = dtReader
    > City.DatatextField = "city"
    > City.DataBind()
    >
    > State.Datasource = dtReader
    > State.DatatextField = "state"
    > State.DataBind()
    >
    > Zip.Datasource = dtReader
    > Zip.DatatextField = "zip"
    > Zip.DataBind()
    > connect.close()
    > ********************************************
    >
    > If this is the case, would this work where I just use a Page DataBind:
    >
    >
    > ********************************************************
    > connect.open()
    > cmdSelect = New OleDbCommand("exec getCustomers",connect)
    > dtReader = cmdSelect.ExecuteReader()
    >
    > Name.Datasource = dtReader
    > Name.DatatextField = "name"
    >
    > Address.Datasource = dtReader
    > Address.DatatextField = "address"
    >
    > City.Datasource = dtReader
    > City.DatatextField = "city"
    >
    > State.Datasource = dtReader
    > State.DatatextField = "state"
    >
    > Zip.Datasource = dtReader
    > Zip.DatatextField = "zip"
    >
    > DataBind()
    > connect.close()
    > ********************************************
    >
    > Also, what was the title of the posts that dealt with this yesterday
    >
    > Thanks,
    >
    > Tom.
    >
    > >
    > > "Cor Ligthert" <> дÈëÏûÏ¢
    > > news:#...
    > >
    > >>Hi Coollzh,
    > >>
    > >>I said this question has been in the adonet group yesterday too, there

    the
    > >>answer was to use an adapter and a datatable, however this is the

    shortest
    > >>way to do it in my opinion with the datareader (for which I see no real
    > >>advantage).
    > >>
    > >>However what is the advantages of using an Ilist solution, I did think

    of
    > >>giving that, however I saw no advantage.
    > >>
    > >>Cor
    > >>
    > >>
    > >>

    > >
    > >

    >
     
    coollzh, Apr 12, 2004
    #9
  10. Thomas Scheiderich

    Cor Ligthert Guest

    Hi Thomas,

    For me there are basicly only 2 methods (assuming you want to use more
    controls in one time)
    - using the datareader and create a collection as the ilist however that
    can also be a datatable
    - using the dataadapter which uses the datareader internally and than the
    datatable enclosed in a dataset and than the dataview to reorder or select
    the data.

    I prefer the last one, with which you can by aspnet easy persist the dataset
    in a session to use them between pages.

    I hope this helps?

    Cor
     
    Cor Ligthert, Apr 12, 2004
    #10
  11. Cor Ligthert wrote:

    > Hi Thomas,
    >
    > For me there are basicly only 2 methods (assuming you want to use more
    > controls in one time)
    > - using the datareader and create a collection as the ilist however that
    > can also be a datatable
    > - using the dataadapter which uses the datareader internally and than the
    > datatable enclosed in a dataset and than the dataview to reorder or select
    > the data.



    Actually, I found another way, although it entails doing multiple
    selects but one trip to the server. I found this yesterday where you
    can send multiple request in the same string separated by semicolons and
    then use datareader.NextResult() to get the next response. The
    drawback, as I mentioned is that it has to do multiple requests. But
    since the request would be the same, I assume the data would be cached
    (or at least the statement would be). I tried it and it works great.
    In the following example, I am filling 3 dropdownboxes with the same set
    of carriers and it does the job.

    ******************************************************************************
    connect.open()
    cmdSelect = New OleDbCommand( _
    "exec populateCarriers;" & _
    "exec populateCarriers;" & _
    "exec populateCarriers",connect)
    dtReader = cmdSelect.ExecuteReader()

    with preferedAirline
    preferedAirline.Datasource = dtReader
    preferedAirline.DataTextField = "carrier"
    preferedAirline.DataBind()
    end with

    dtReader.NextResult()

    with preferedAirline2
    preferedAirline2.Datasource = dtReader
    preferedAirline2.DataTextField = "carrier"
    preferedAirline2.DataBind()
    end with

    dtReader.NextResult()

    with preferedAirline3
    preferedAirline3.Datasource = dtReader
    preferedAirline3.DataTextField = "carrier"
    preferedAirline3.DataBind()
    end with

    connect.close()
    ***********************************************************

    Thank,

    Tom.

    >
    > I prefer the last one, with which you can by aspnet easy persist the dataset
    > in a session to use them between pages.
    >
    > I hope this helps?
    >
    > Cor
    >
    >
    >
    >
     
    Thomas Scheiderich, Apr 12, 2004
    #11
  12. Thomas Scheiderich

    Cor Ligthert Guest

    Hi Thomas,

    See under the message.

    > > For me there are basicly only 2 methods (assuming you want to use more
    > > controls in one time)
    > > - using the datareader and create a collection as the ilist however

    that
    > > can also be a datatable
    > > - using the dataadapter which uses the datareader internally and than

    the
    > > datatable enclosed in a dataset and than the dataview to reorder or

    select
    > > the data.

    >
    >
    > Actually, I found another way, although it entails doing multiple
    > selects but one trip to the server. I found this yesterday where you
    > can send multiple request in the same string separated by semicolons and
    > then use datareader.NextResult() to get the next response. The
    > drawback, as I mentioned is that it has to do multiple requests. But
    > since the request would be the same, I assume the data would be cached
    > (or at least the statement would be). I tried it and it works great.
    > In the following example, I am filling 3 dropdownboxes with the same set
    > of carriers and it does the job.
    >


    This is for me the datareader method which I do not like because it is
    limited, than my sample is much more effective I think.

    (You only found a way to write it in one string however the result is the
    same in my opinion).

    You probably don't know how many alternatives there are using the datatable.

    Cor
     
    Cor Ligthert, Apr 13, 2004
    #12
    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. Rob Meade

    reusing a bit of code?

    Rob Meade, Jan 30, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    361
    Kevin Spencer
    Jan 30, 2004
  2. Dan
    Replies:
    4
    Views:
    9,539
    taylorjonl
    Jun 1, 2007
  3. anon

    Reusing DataSet/DataTable

    anon, Apr 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    501
  4. Scott Allen

    Re: reusing codebehind files?

    Scott Allen, Apr 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    359
    Anders Bohlin
    Apr 13, 2004
  5. powerranger

    reusing a class

    powerranger, Aug 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    341
    Andy Fish
    Aug 11, 2004
Loading...

Share This Page