473,396 Members | 1,864 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,396 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 14438
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.