473,383 Members | 1,978 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,383 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 4962
"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...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?

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.