469,887 Members | 1,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

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 2223

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by dazzalondon | last post: by
5 posts views Thread by KivanÁ Karaca | last post: by
2 posts views Thread by Cameron Frasnelly | last post: by
2 posts views Thread by squid | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.