473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Runtime error: Invalid attempt to read when no data is present

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 FROM TimeSlot;"
cmd = New SqlCommand(sqlStmt, conn)
cmd.Connection.Open()
dr = cmd.ExecuteReader
Dim s As Integer = CInt(dr("t")) 'Runtime error: Invalid attempt to read
when no data is present.
any ideas?? please advise!
Nov 18 '05 #1
2 2352

dr = cmd.ExecuteReader
Dim s As Integer
if Not dr.IsDBNull(0) then
s = dr.GetInt32(0)
EndIf

- Paul Glavich
-----Original Message-----
I want to read how many records in the table, and insert a record with idfield which increment the counter by 1. However, I had the following runtimeon Dim s As Integer = CInt(dr("t")). Since "t" (I want to represent thecount, but just a tempoary variable, not a field in table) doesnt exist inthe table
sqlStmt = "SELECT COUNT(*) As t FROM TimeSlot;"
cmd = New SqlCommand(sqlStmt, conn)
cmd.Connection.Open()
dr = cmd.ExecuteReader
Dim s As Integer = CInt(dr("t")) 'Runtime error: Invalid attempt to readwhen no data is present.
any ideas?? please advise!
.

Nov 18 '05 #2
Hi,

You should use GetInt32 method to get integer value and you should call Read
before starting fetching data. Also you need to specify column index, not
name

Dim s As Integer
While dr.Read
s = dr.GetInt32(0)
end While

Anyway I do not like an idea to handle identity field this way.Why not to
declare your actual field as an IDENTITY in a database? I this case you
should not worry about next ID. Otherwise you could get into trouble if some
records are deleted from the table. For example, If you had two records with
IDs 1 and 2 and then deleted record with ID 1, your SQL statement will
return count 1 and next ID will be 2, but you already have it.

--
Val Mazur
Microsoft MVP
Check Virus Alert, stay updated
http://www.microsoft.com/security/incident/blast.asp
"Matthew Louden" <ma*******@hotmail.com> wrote in message
news:uX**************@tk2msftngp13.phx.gbl...
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 FROM TimeSlot;"
cmd = New SqlCommand(sqlStmt, conn)
cmd.Connection.Open()
dr = cmd.ExecuteReader
Dim s As Integer = CInt(dr("t")) 'Runtime error: Invalid attempt to read
when no data is present.
any ideas?? please advise!

Nov 18 '05 #3

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

Similar topics

2
5195
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....
1
1206
by: dazzalondon | last post by:
Hi there. I have various pages in an ASP.NET applicaiton and in the Page_load event I have data being read from a SQL DB filling textboxes on the page Now, sometimes the fields in the DB will...
5
1363
by: Kivanç Karaca | last post by:
Hi all ! I am pretty sure this is an easy one but I couldnt find a way to fix it. How can I get rid of the asp.net error saying "Invalid attempt to read when no data is present. " when there is...
2
2916
by: Cameron Frasnelly | last post by:
I emulated the code from the .Net Framework help (Titled "Using Stored Procedures with a Command") and I still receive and error... Error Received = "Invalid attempt to read when no data is...
2
1518
by: squid | last post by:
First off, I'm a python neophyte, but I'm fairly experienced with Java, C and PHP. I've been trying to use the xmlrpclib to perform remote calls against a service, and it works nicely. However,...
4
3464
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
15428
by: dcwbxb | last post by:
Trying to create a program which reads and echos the contents of an input data file, which consists of two records. When I complile my code I keep getting a "error C2001: newline in constant" error....
1
1879
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...
2
2449
by: vijayrvs | last post by:
SearchCrawler.java The program search crawler used to search the files from the website. From the following program i got 7 compiler error. can any body clarify it and provide me solution. ...
0
7260
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
7384
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
7539
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
7525
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...
1
5089
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...
0
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
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 ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.