473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.InvalidO perationExcepti on: Invalid attempt to read
when no data is present.

Source Error:
Line 42: Dim rs As SqlDataReader = sqlCmd.ExecuteR eader()
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 =
ConfigurationMa nager.Connectio nStrings("SIPco nnectionstring" ).ConnectionStr ing
sqlConn = New SqlConnection(s trConnection)
sqlCmd = New SqlCommand("Sel ect * from documentos where
idpropuesta='" & Request.QuerySt ring("mivar") & "'", sqlConn)

sqlConn.Open()

resul = sqlCmd.ExecuteN onQuery
Dim rs As SqlDataReader = sqlCmd.ExecuteR eader()
rs.Read()
Response.Write( rs("Nombre"))
Nov 11 '08 #1
6 4979
"egsdar" <eg****@discuss ions.microsoft. comwrote in message
news:2C******** *************** ***********@mic rosoft.com...
How can I solve this error:
By using an SQL statement which returns data.
Select * from documentos where idpropuesta='" &
Request.QuerySt ring("mivar")
For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QuerySt ring 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****@discuss ions.microsoft. comwrote in message
news:2C******** *************** ***********@mic rosoft.com...
How can I solve this error:

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

For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QuerySt ring 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****@discuss ions.microsoft. comwrote in message
news:78******** *************** ***********@mic rosoft.com...
>For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QuerySt ring 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.InvalidO perationExcepti on: Invalid attempt to read
when no data is present.

Source Error:
Line 42: Dim rs As SqlDataReader = sqlCmd.ExecuteR eader()
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 =
ConfigurationMa nager.Connectio nStrings("SIPco nnectionstring" ).ConnectionStr ing
sqlConn = New SqlConnection(s trConnection)
sqlCmd = New SqlCommand("Sel ect * from documentos where
idpropuesta='" & Request.QuerySt ring("mivar") & "'", sqlConn)

sqlConn.Open()

resul = sqlCmd.ExecuteN onQuery
Dim rs As SqlDataReader = sqlCmd.ExecuteR eader()
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****@discuss ions.microsoft. comwrote in message
news:78******** *************** ***********@mic rosoft.com...
For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QuerySt ring 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****@discuss ions.microsoft. comwrote in message
news:25******** *************** ***********@mic rosoft.com...
>>>For whatever reason, the above SQL statement does not return any rows,
almost certainly because Request.QuerySt ring 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.ExecuteN onQuery
Dim rs As SqlDataReader = sqlCmd.ExecuteR eader()

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
5200
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. ---------------------------------------------------------------------------- ---- Invalid attempt to read when no data is present. Description: An...
2
2357
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" (I want to represent the count, but just a tempoary variable, not a field in table) doesnt exist in the table sqlStmt = "SELECT COUNT(*) As t...
0
683
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. Aparently, when I select a person, it's not selecting anyone and returning this error. Here's the full error: Description: An unhandled exception...
4
12349
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 present which is correct. The line works when data is in the row. But shouldn't the
0
4708
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 the client application establish a connection, and send data, which appears in plain, de-crypted text on the server - this works.
4
3466
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 values I get "Invalid attempt to read when no data is present". private void GetLotIDPriorityFromLot(string aLotDesc, out int aLotID, out...
3
6514
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 share ur knowledge to help me out with this probs. my code is as below,
2
1754
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" keeps showing and i don't know why...i executed my stored procedure in the query analyzer and it's ok, i'm using the sentence "mydatareader.Read()" and...
1
1881
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 another page the script works yielding the desired records BUT WHEN YOU CALL THE SCRIPT AGAIN WHEN ATTEMPTING TO CALL THE SAME PAGE (PASSING...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.