Invalid attempt to read when no data is present

  • Thread starter Thread starter egsdar
  • Start date Start date
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"))
 
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?
 
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)
 
Back
Top