473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to read the LAST record of datareader?

Tom
Hi

How to read the LAST record of datareader? Here is my code

[code

StringBuilder objBuilder = new StringBuilder()
objBuilder.Append("SELECT startdate, enddate FROM records where userid = '")
objBuilder.Append(id)
objBuilder.Append("'")

SqlDataReader dr = null
int RecordCount = 0

tr

dr = SqlHelper.ExecuteReader(DBConnection.ConnString, CommandType.Text, objBuilder.ToString())

while (dr.Read()

RecordCount++
if(dr.) // how to read the LAST record of the selected rows

this.StartDate = Convert.ToDateTime(dr["startdate"])
this.EndDate = Convert.ToDateTime(dr["enddate"])

[/code

I know how to count the total number of row. But, I only need the last row of data. dr.Read() gets the first row only. How can I do it

Thanks for hel

Nov 16 '05 #1
1 14403
Tom <ke*****@yahoo.com> wrote:
I know how to count the total number of row. But, I only need the
last row of data. dr.Read() gets the first row only. How can I do it?


I would suggest reversing the order of your query (which is currently
unordered as far as I can see - selecting just the last result could
give you a different record each time) and using SELECT TOP 1. Then you
know you'll only get one record.

I'd also highly recommend using a parameter instead of adding a string
directly into the SQL.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BenM | last post by:
I am a novice user of ASP.net, so the following might be trivial for many of you. I am trying to iterate through a datareader object and add rows to a table based on the recordset. Here is my...
1
by: Tom | last post by:
Hi How to read the LAST record of datareader? Here is my code ) this.EndDate = Convert.ToDateTime(dr) [/code
5
by: D. Shane Fowlkes | last post by:
This may be a very basic question but it's something I've never done before. I've looked at a couple of my favorite sites and books and can't find an answer either. I can write a Function to...
5
by: JenHu | last post by:
Hi experts, I wrote a function which retrieves a file in the folder, the file path is : Dim sr As New StreamReader(strFilepath & ReturnFileName) What if I have more than 1 file_name in...
15
by: mosipenko | last post by:
I used to have a problem with DBNull, but now my issue is: System.InvalidOperationException: Invalid attempt to read when no data is present Here's my code behind: Private Sub...
2
by: rn5a | last post by:
The following code calls a function in a DLL which returns a SqlDataReader (the function in the DLL calls a stored procedure which returns 6 columns): Dim boCart As Cart Dim sqlReader As...
2
by: shapper | last post by:
Hello, I have the following code: 1 ' Code that creates the connection, parameters, etc 2 ' ... 3 4 ' Execute the command 5 6 ' ???? THE CODE I NEED TO KNOW
1
by: IndiraPriyaDarshini | last post by:
Hi, I have a datareader, and i wanna read its first record and then only i want it in a loop. when i read its first record, i cannot start the reder again. hw ca i solve this pblm.pls help...
3
by: Osamede.Zhang | last post by:
I have some code like this: SqlCommand cmd = new SqlCommand("get_storeid_byuser", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.