Invalid attempt to read when no data is present

E

egsdar

How can I solve this error:
Invalid attempt to read when no data is present.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Invalid attempt to read
when no data is present.

Source Error:


Line 42: Dim rs As SqlDataReader = sqlCmd.ExecuteReader()
Line 43: rs.Read()
Line 44: Response.Write(rs("Nombre"))
Line 45: Response.End()
Line 46: %>

This is my code:

Dim sqlConn As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConnection, resul As String
strConnection =
ConfigurationManager.ConnectionStrings("SIPconnectionstring").ConnectionString
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("Select * from documentos where
idpropuesta='" & Request.QueryString("mivar") & "'", sqlConn)

sqlConn.Open()

resul = sqlCmd.ExecuteNonQuery
Dim rs As SqlDataReader = sqlCmd.ExecuteReader()
rs.Read()
Response.Write(rs("Nombre"))
 
E

egsdar

Yes, you're right about the SQL injection, however I have to focus on the
results, then I'll create stored procedures to help me on that, the thing is
that i have to present this tomorrow and I don't have to much time.
The query is fine I already test and it brings data if I use it in the sql
query.
So, what's wrong?
 
B

bruce barker

not sure why the ExecuteNonQuery followed by ExcuteReader which will run
the query twice.

the rs.Read() return true or false depending on whether a row was read
or not. if false, then accessing row data will throw an error.

-- bruce (sqlwork.com)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top