473,385 Members | 1,355 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,385 software developers and data experts.

datareader questions

Can someone please explain this to me. I am not understanding why this
works the way it does. The commented lines are the problems

Dim oleEmp as OleDbCommand
Dim myFinds as OleDataReader
Dim strSearch as String
Dim strFound as String

strSearch = "select * from employee where [first name]='" &
txtName.Text.ToString & "'"

oleEmp = New OleDbCommand(strSearch, oleConn)

Try

'*********************************
' Why does the next line of code not fill the datareader
'*********************************
myFinds = oleEmp.ExecuteReader

'*********************************
' in order for it to work I had to perform a read
'myFinds.Read()
'as-is the code throws up an exception
'
' No Data exists for the row/column.
'*********************************
strFound = myFinds.Item(2)
MsgBox(txtName.Text.ToString + "'s last name is " + strFound

Catch prob as Exception

MsgBox(prob.Message)

End Try

If I uncomment the myFinds.Read() line then everything works fine but I'm
just not understand why it needs to read. It seems to me that when the
myFinds = oleEmp.ExecuteReader executes the datareader should have info to
use. Like I said this is more of a curiousity than a problem.

And now for my question: Does the datareader return a count of the records
that were returned? From the above code you can see why I want to know if
there is more than one record returned.

Thanks


Nov 20 '05 #1
3 2138
In article <Zo******************@bignews4.bellsouth.net>, William wrote:
Can someone please explain this to me. I am not understanding why this
works the way it does. The commented lines are the problems

Dim oleEmp as OleDbCommand
Dim myFinds as OleDataReader
Dim strSearch as String
Dim strFound as String

strSearch = "select * from employee where [first name]='" &
txtName.Text.ToString & "'"

oleEmp = New OleDbCommand(strSearch, oleConn)

Try

'*********************************
' Why does the next line of code not fill the datareader
'*********************************
myFinds = oleEmp.ExecuteReader

'*********************************
' in order for it to work I had to perform a read
'myFinds.Read()
'as-is the code throws up an exception
'
' No Data exists for the row/column.
'*********************************
strFound = myFinds.Item(2)
MsgBox(txtName.Text.ToString + "'s last name is " + strFound

Catch prob as Exception

MsgBox(prob.Message)

End Try

If I uncomment the myFinds.Read() line then everything works fine but I'm
just not understand why it needs to read. It seems to me that when the
myFinds = oleEmp.ExecuteReader executes the datareader should have info to
use. Like I said this is more of a curiousity than a problem.

And now for my question: Does the datareader return a count of the records
that were returned? From the above code you can see why I want to know if
there is more than one record returned.

Thanks


The reason your seeing the behavior you are is that a DataReader (class
that implements IDataReader) is a forward-only stream based object. In
other words, unlike using a DataAdpater, you are using a connected
datasource. All the ExecuteReader method does, is construct a handle to
this datasource. The usual way of using a reader is something like:

myReader = myCommand.ExecuteReader()

Do While (myReader.Read())
' do stuff with the data
Loop

This can execute 0 to n Times depending on the number of rows
returned... As for getting the row count - that is not supported by the
IDataReader interface. I suppose it would be possible for a individual
datasource to implement an extension to the actual object returned (but,
I don't believe the OleDB provider does this). But, it would not be
part of the generic interface and would be datasource dependent... The
closest thing is the RecordsAffected property - and that is the number
of rows inserted, changed, or deleted by an SQL statement. Not at all
what your looking for :)

--
Tom Shelton [MVP]
Nov 20 '05 #2

"William" <an******@microsoft.com> wrote in message
news:Zo******************@bignews4.bellsouth.net.. .
Can someone please explain this to me. I am not understanding why this
works the way it does. The commented lines are the problems

Dim oleEmp as OleDbCommand
Dim myFinds as OleDataReader
Dim strSearch as String
Dim strFound as String

strSearch = "select * from employee where [first name]='" &
txtName.Text.ToString & "'"

oleEmp = New OleDbCommand(strSearch, oleConn)

Try

'*********************************
' Why does the next line of code not fill the datareader
'*********************************
myFinds = oleEmp.ExecuteReader

'*********************************
' in order for it to work I had to perform a read
'myFinds.Read()
'as-is the code throws up an exception
'
' No Data exists for the row/column.
'*********************************
strFound = myFinds.Item(2)
MsgBox(txtName.Text.ToString + "'s last name is " + strFound

Catch prob as Exception

MsgBox(prob.Message)

End Try

If I uncomment the myFinds.Read() line then everything works fine but I'm
just not understand why it needs to read. It seems to me that when the
myFinds = oleEmp.ExecuteReader executes the datareader should have info to use. Like I said this is more of a curiousity than a problem.

And now for my question: Does the datareader return a count of the records that were returned? From the above code you can see why I want to know if
there is more than one record returned.


As Tomn said no. If you need to determine how many records are affected by a
given SELECT statement you should try ExecuteScaler with a count statement.
If you just want to know whether the reader has any records in it you use
the statement

If dr.read then

which returns a boolean.

The reason you need to call .read is that when the datareader is returned
the forward only cursor is set to before the first record... think of it as
being parked. For returning datareaders i use the same statement over and
over which covers most of my basis

dr = myCommand.ExecuteReader(CommandBehaviour.CloseConn ection)

if dr is nothing orelse not dr.read then .... return or whatever

You should also be checking for a Null condition from the field you are
trying to read before you read it i.e

If not IsDbNull(myFinds.Item(2)) then
strFindFind = cstr(myFinds.Items(2))
End if
hth
Richard
Nov 20 '05 #3
Don't forget that version 1.1 of .NET Framework added the HasRows property
to the datareader.

Greg
If you just want to know whether the reader has any records in it you use
the statement

If dr.read then

which returns a boolean.

Nov 20 '05 #4

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

Similar topics

0
by: Mike | last post by:
I am trying to use a datareader to retrieve data and set some properties. I am getting a "Specified method is not supported" error on Line xxx. The data base is Oracle 9i and the field is a...
5
by: Rob Wire | last post by:
For the code below, how could I add an item in the drop down lists for both company and location to be an "All" selection that would send to the stored proc. spRptAttachments a value of "%" so...
2
by: simon | last post by:
Always when I need data reader in my programs, I simply have functions, which creates it for me: Dim rdr As SqlDataReader dim sql as string sql="myStoredProcedure" rdr =...
7
by: Bart Schelkens | last post by:
Hi, I have 2 more questions : 1. Can I fill a datagrid by using a DataReader or does it have to be a DataSet or a DataView? 2. In my datagrid I need to display two images and one of does...
2
by: Andrei Pociu | last post by:
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place where I initialize the SqlConnection. Also...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
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...
3
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to add a datagridview control to a Windows Form to display read-only information in visual basic 2005. My understanding is that datareader will be faster for this purpose. I have the...
2
Pittaman
by: Pittaman | last post by:
Hello, Sorry about the title, I had a hard time explaining this. I've done a little trick to solve a problem I was having with this third party control, and was wondering if anyone here could...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.