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

Invalid attempt to read when no data is present

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("SIPconnect ionstring").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"))
Nov 11 '08 #1
6 4964
"egsdar" <eg****@discussions.microsoft.comwrote in message
news:2C**********************************@microsof t.com...
How can I solve this error:
By using an SQL statement which returns data.
Select * from documentos where idpropuesta='" &
Request.QueryString("mivar")
For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QueryString is incorrect.

Also, it's important that you realise that you are wide open to SQL
Injection - Google it...

What would happen if someone modified the URL manually to change the
querystring as follows...

http://www.mysite.com/mypage.aspx?mivar=1;DELETE FROM documentos
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 11 '08 #2
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?
"Mark Rae [MVP]" wrote:
"egsdar" <eg****@discussions.microsoft.comwrote in message
news:2C**********************************@microsof t.com...
How can I solve this error:

By using an SQL statement which returns data.
Select * from documentos where idpropuesta='" &
Request.QueryString("mivar")

For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QueryString is incorrect.

Also, it's important that you realise that you are wide open to SQL
Injection - Google it...

What would happen if someone modified the URL manually to change the
querystring as follows...

http://www.mysite.com/mypage.aspx?mivar=1;DELETE FROM documentos
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 11 '08 #3
"egsdar" <eg****@discussions.microsoft.comwrote in message
news:78**********************************@microsof t.com...
>For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QueryString is incorrect.

The query is fine I already test and it brings data if I use it in the sql
query.
So, what's wrong?
It's not returning any data.

Run a SQL trace and inspect what's *actually* being sent to SQL Server...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 11 '08 #4
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)

egsdar wrote:
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("SIPconnect ionstring").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"))
Nov 11 '08 #5
Actually, when I run the same query in the SQL Query Analyzer brings data.
"Mark Rae [MVP]" wrote:
"egsdar" <eg****@discussions.microsoft.comwrote in message
news:78**********************************@microsof t.com...
For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QueryString is incorrect.
The query is fine I already test and it brings data if I use it in the sql
query.
So, what's wrong?

It's not returning any data.

Run a SQL trace and inspect what's *actually* being sent to SQL Server...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 11 '08 #6
"egsdar" <eg****@discussions.microsoft.comwrote in message
news:25**********************************@microsof t.com...
>>>For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QueryString is incorrect.

The query is fine I already test and it brings data if I use it in the
sql
query.
So, what's wrong?

It's not returning any data.

Run a SQL trace and inspect what's *actually* being sent to SQL Server...

Actually, when I run the same query in the SQL Query Analyzer brings data.
I'm sure it does. However, that's not what I asked you to do...

You need to run a SQL trace. This will allow you to inspect what is
*actually* being sent to the SQL Server, irrespective of what you think is
being sent, or what you think should be sent.

Also, following up on Bruce's reply, is there any reason that you're doing
this:

resul = sqlCmd.ExecuteNonQuery
Dim rs As SqlDataReader = sqlCmd.ExecuteReader()

ExecuteNonQuery is used specifically to send commands to a database which
*DON'T* return any records e.g. INSERTs, UPDATEs, DELETEs, DDL etc.
http://msdn.microsoft.com/en-us/libr...enonquery.aspx
What is its purpose here?

See here: http://msdn.microsoft.com/en-us/library/9kcbe65k.aspx for an
example of how to return a DataReader - there is no mention of
ExecuteNonQuery...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 11 '08 #7

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

Similar topics

2
by: Brent Burkart | last post by:
Below is the error I am receiving. I have checked SQL Profiler and it is receiving the correct query which runs fine in Query Analyzer. Any ideas? Server Error in '/lockinsheet' Application....
2
by: Matthew Louden | last post by:
I want to read how many records in the table, and insert a record with id field which increment the counter by 1. However, I had the following runtime on Dim s As Integer = CInt(dr("t")). Since "t"...
0
by: Jerry | last post by:
Below is ALL the code for all the databases... Here's the problem: I callup the aspx file in IE and the form comes up just fine. When I select a person to update, I get the subject error. ...
4
by: Dave | last post by:
I'm using a datareader to get data from an sql table. The line that gives the error is as follow, dtrReceivers.ToString() which gives the error, Invalid attempt to read when no data is...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
4
by: MarkusR | last post by:
If I run the stored proc in the Query Analyzer this works and I get the expected result set back. However when I run it in my application I get a results set of one row but when I try to access the...
3
by: divsTiw | last post by:
I want to populate combo box with data from OracleDataReader , but "Invalid attempt to read when no data is present." is thrown. there are two rows returned , then too why such error. plzzz...
2
by: Naty | last post by:
please can anybody help me? i'm trying to retrieve data from a db with visual studio 2005 and sql server 2000 in a web application, and the error "Invalid attempt to read when no data is present"...
1
Oodles Of Noodles
by: Oodles Of Noodles | last post by:
Hello fellow geeks I have a problem in my database iVB .Net program that is generating 'Error:Invalid attempt to read when no data is present.' The weird part is that when you call the page from...
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: 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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.