Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Data Reader in ASP.NET

  1. #1
    Join Date
    Jul 2006
    Posts
    276
    Rep Power
    0

    Default Data Reader in ASP.NET

    Is there any way with a .NET DataReader to tell when you are working with the last record? Something like EOF in classic ASP.

    Well i know how to traverse through a datareader and i know that it stops automatically when it reaches EOF but what i really want to know is when it reaches the penultimate record (the record before the last) so I can conduct some actions there!

    NEED URGENT HELP!
    Last edited by norminator; Aug 9, 2007 at 11:04 AM.
    The greatest discovery of all men through lifetime is that a man can alter his life by altering his attitude!

  2. #2
    Join Date
    Sep 2004
    Posts
    681
    Rep Power
    0

    Default

    Quote Originally Posted by norminator View Post
    Is there any way with a .NET DataReader to tell when you are working with the last record? Something like EOF in classic ASP.
    None that I can think of. A DataReader has no concept of the number of records being passed through the underlying connection [on a SELECT operation]. A DataReader works well for firehose operations but if you need to manipulate data you need to use an intermediary like a DataSet.

  3. #3
    Join Date
    Feb 2005
    Posts
    85
    Rep Power
    0

    Default

    Quote Originally Posted by norminator View Post
    Is there any way with a .NET DataReader to tell when you are working with the last record? Something like EOF in classic ASP.


    NEED URGENT HELP!

    try something like this:

    Code:
    querycount = "SELECT COUNT(primary_key) AS RecordCount FROM table1;"
    com.commandText=querycount
    
    reader = com.ExecuteReader()
    
    Dim recordcount as integer
    recordcount = reader("RecordCount")
    
    reader.close
    
    myquery="SELECT * FROM table1"
    com.commandText=myquery
    
    reader = com.ExecuteReader()
    
    mycount=0
    do while reader.read
    	if mycount=recordcount then
    		msgbox "Last record Reached"	
    	end if
    	mycount=mycount+1
    loop
    Last edited by Arch_Angel; Aug 10, 2007 at 09:32 AM. Reason: added CODE tags
    Code King aka Code WizZzard: Motivated By The Challenge, Driven By The Will To Succeed.

    In The Land Of Programmers, The Code WizZzard Is KING. Sen on anything VB

  4. #4
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by ToxXxic View Post
    try something like this:

    querycount = "SELECT COUNT(primary_key) AS RecordCount FROM table1;"
    com.commandText=querycount

    reader = com.ExecuteReader()

    Dim recordcount as integer
    recordcount = reader("RecordCount")

    reader.close

    myquery="SELECT * FROM table1"
    com.commandText=myquery

    reader = com.ExecuteReader()


    that suggestion definitely works... the only thing i'd change is to use a stored procedure instead, or put both statements in one query, read one result which tells the count then the other tells the actual data.

    Code:
            Dim cmd As New Data.SqlClient.SqlCommand("SELECT COUNT(*) AS RecordCount FROM table1;SELECT * FROM table1;", cnnStr)
    
            Dim reader As Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
    
            If Not reader.Read() Then Return ' no count ... probably should throw exception
    
            Dim recordcount As Integer = reader("RecordCount")
    
            If Not reader.NextResult() Then Return ' no data ... probably should throw exception
    
            ' read the data just as before
    by the way ... if that is for ASP and not ASP.NET then check this out... or just check it out anyway, it has the sp i was talking about
    Last edited by icymint3; Aug 9, 2007 at 05:09 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  5. #5
    Join Date
    Sep 2004
    Posts
    681
    Rep Power
    0

    Default

    Quote Originally Posted by ToxXxic View Post
    try something like this:

    querycount = "SELECT COUNT(primary_key) AS RecordCount FROM table1;"
    com.commandText=querycount
    <snip>
    I see where you are going with this but it seems like a bit of a kluge that has the danger of returning inconsistent results. The code snippet executes two queries in two separate roundtrips to the server. icymint3's suggestion would get around that but if the data has to be read through anyway, filling a DataSet probably makes more sense.

  6. #6
    Join Date
    Jul 2006
    Posts
    276
    Rep Power
    0

    Default yeah

    Well i've done it using a dataset already i was basically looking for an easie rand faster way of doing it.

    Here is the code:

    Code:
    sql = " SELECT extension FROM tblExtensions INNER JOIN tblDepartment ON          tblExtensions.DepartmentNum =" & _
           " tblDepartment.DepartmentID WHERE DepartmentNuM =" & dept
    
    con = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source= C:\Inetpub\wwwroot\CallAccounting\Members\members.mdb")
            \con.Open()
            daExt = New OleDbDataAdapter(sql, con)
            daExt.Fill(dsExt, "tblExtensions")
    
     ddlExtension.DataSource = dsExt
            ddlExtension.DataTextField = "extension"
            ddlExtension.DataBind()
    
    con.Close()
    Last edited by Arch_Angel; Aug 10, 2007 at 09:33 AM. Reason: added CODE tags
    The greatest discovery of all men through lifetime is that a man can alter his life by altering his attitude!

  7. #7
    Join Date
    Sep 2004
    Posts
    681
    Rep Power
    0

    Default

    Quote Originally Posted by norminator View Post
    Well i've done it using a dataset already i was basically looking for an easie rand faster way of doing it.
    When you say you are looking for something faster is it that you take a significant performance hit when using a DataSet or are you just referring to syntax?

    As an aside, have you considered moving to some flavour of [desktop] SQL Server like MSDE or SQL Server Express or was there a reason for sticking with the Access format?

  8. #8
    Join Date
    Jul 2004
    Posts
    153
    Rep Power
    0

    Default

    Dude honestly if ur objective is get speed in spitting out the data then the datareader would be best. However this would best accomplished if you use a SP as recommended by IcyMint...(i know this is an unnecessary post, i'm just trying to re-enforce the point)

  9. #9
    Join Date
    Sep 2004
    Posts
    681
    Rep Power
    0

    Default

    Quote Originally Posted by codecarnage View Post
    Dude honestly if ur objective is get speed in spitting out the data then the datareader would be best. However this would best accomplished if you use a SP as recommended by IcyMint...(i know this is an unnecessary post, i'm just trying to re-enforce the point)
    Take a look at the code snippet. Unfortunately stored procedures don't exist for Access databases and that is what is being used. DataReaders are ideal for just getting the data out but if you want to do any kind of in-code data transformation then a DataSet would be the way to go.

    If some flavour of SQL Server is used then the option of using a stored procedure to manipulate data prior to being returned opens up. Of course moving away from the Access database format should also realize a whole heaping helping of performance gains too.

    ...which brings me back to my initial question. Why use the Access format?

  10. #10
    Join Date
    Jul 2006
    Posts
    276
    Rep Power
    0

    Default reason

    Quote Originally Posted by Goucham View Post
    When you say you are looking for something faster is it that you take a significant performance hit when using a DataSet or are you just referring to syntax?

    As an aside, have you considered moving to some flavor of [desktop] SQL Server like MSDE or SQL Server Express or was there a reason for sticking with the Access format?
    Well yute I know using a data reader is much better in terms of performance and as you said access doesn't uses stored procedures, And yes its for my organization I'm updating a software that uses access, not everyone has sql server so its easier to deploy a software using access.
    The greatest discovery of all men through lifetime is that a man can alter his life by altering his attitude!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •