473,654 Members | 3,033 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(sqlS tmt, conn)
cmd.Connection. Open()
dr = cmd.ExecuteRead er
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 2366

dr = cmd.ExecuteRead er
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(sqlS tmt, conn)
cmd.Connection .Open()
dr = cmd.ExecuteRead er
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*******@hotm ail.com> wrote in message
news:uX******** ******@tk2msftn gp13.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(sqlS tmt, conn)
cmd.Connection. Open()
dr = cmd.ExecuteRead er
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
5205
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 unhandled exception occurred during the execution of the current web request. Please review the...
1
1215
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 be blank and I would a 'Invalid attempt to read when no data is present.' error. I have tried to implement validation in pages to ease this issue but I need a solution which covers just the below - reading a DB where it doesn't fail (I have...
5
1373
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 no data available? What should I do ?? Thanks KK
2
2928
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 present." BUT when I manually run the Stored Procedure and manually type in '3333' it works just fine by returning the single row for that particular work order???????? *****More Clues****
2
1527
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, due to my lack of python-knowledge, I'm rather puzzled at the way the class works. Specifically, suppose I have something like this snippet of code: from xmlrpclib import ServerProxy
4
3469
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 DateTime aPriorityDate) { SqlConnection Conn; SqlDataReader reader = null;
3
15441
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. Read every forum regarding the "error C2001: newline in constant" error but I am still not clear as to where my code is erroring out. Can anyone help point me in the right direction? Thanks My code: ...
1
1888
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 DIFFERENT PARAMETERS USING QUERYSTRING ) THE QUERY DOESN'T YIELD ANY DATA Sub PropertyView( )
2
2454
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. import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*;
0
8370
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8590
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5620
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
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
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.