get values from query

G

Guest

i have a simple query
SELECT User_Pass,id FROM Users
and i want using ExecuteReader to get the values of this 2 columns into
parameters.
when i try to do :
Dim reader As SqlDataReader = myCommand.ExecuteReader()
and then
reader.GetString(0) i get :
Invalid attempt to read when no data is present
while for suer i know that there is a data
what to do?
thnaks in advane
peleg
 
M

Mark Rae [MVP]

pelegk1 said:
i have a simple query
SELECT User_Pass,id FROM Users
and i want using ExecuteReader to get the values of this 2 columns into
parameters.
when i try to do :
Dim reader As SqlDataReader = myCommand.ExecuteReader()
and then
reader.GetString(0) i get :
Invalid attempt to read when no data is present
while for suer i know that there is a data
what to do?


You've forgotten to call the Read method on the SqlDataReader:
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

Dim reader As SqlDataReader = myCommand.ExecuteReader()
reader.Read()
reader.GetString(0)
 
P

Phil H

i have a simple query
SELECT User_Pass,id FROM Users
and i want using ExecuteReader to get the values of this 2 columns into
parameters.
when i try to do :
Dim reader As SqlDataReader = myCommand.ExecuteReader()
and then
reader.GetString(0) i get :
Invalid attempt to read when no data is present
while for suer i know that there is a data
what to do?
thnaks in advane
peleg

You must execute the statement

reader.Read

before attempting to access the results with GetString etc. The
ExecuteReader method only creates a reader object, it doesn't retrieve
any data.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top