473,382 Members | 1,389 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,382 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 14429
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.