Invalid attempt to read when no data is present.

Mohan

Member
Joined
Oct 18, 2006
Messages
17
Location
bangalore
Programming Experience
1-3
Dim strQRY As String = "Select Max(id) From DESIGNATION"
Dim conTemp As New SqlConnection
conTemp.ConnectionString = CONNECTIONSTRING
conTemp.Open()
Dim command As New SqlCommand(strQRY, conTemp)
Dim reader As SqlDataReader = command.ExecuteReader()
Return reader(0)


when i query the same thing i'm getting result in sqlserver but above code giving pbm
 
Don't use a DataReader to get a single value. Call the ExecuteScalar method of the Command to get an Object that contains the single result. If the Object is Nothign then there was no value returned by the query.
 
Dim strQRY AsString = "Select Max(id) From DESIGNATION"
Dim conTemp AsNew SqlConnection
conTemp.ConnectionString = CONNECTIONSTRING
conTemp.Open()
Dim command AsNew SqlCommand(strQRY, conTemp)
Dim val As Object = command.ExecuteScalar()
'do validation of the returned result here
Return (val or some derivation)


like that! :)
 
Back
Top