Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SqlDataReader and 'invalid data attempt when no data present' error

Status
Not open for further replies.

dkim18

Technical User
Feb 13, 2004
5
US
hi!

I am trying to excute sql command and get result.
The following code gives me an error, 'invalid data attempt when no data present'
The result that I am looking for is integer. Do I need to use something other than SqlDataReader?

Thanks in advance

----------------------------------------------------------
Try

objConn.Open()

Dim objCmd As SqlCommand = objConn.CreateCommand()

objCmd.CommandText = _
"SELECT max(SessionNum) FROM TreadMills WHERE UserID = '" & ID & "'"
Dim dataReader As SqlDataReader = objCmd.ExecuteReader()
MessageBox.Show("dataReader is " & dataReader.GetInt32(0))<-----------------error
Catch ex As Exception
MessageBox.Show("Error msg " & ex.Message)
-------------------------------------------------------
 

You could use the isNull() function to return a zero to ensure at least one record is returned when no sessionNum records exist in TreadMills for the specified UserID.

(I also like to specify a column name for aggregate columns...)

"SELECT [green]isNull([/green]max(SessionNum)[green],0) as MaxSessionNum[/green] FROM TreadMills WHERE UserID = '" & ID & "'"



Mark

&quot;You guys pair up in groups of three, then line up in a circle.&quot;
- Bill Peterson, a Florida State football coach
 
Thank you for your info, but I am still having same err msg. My question was how to get integer result from excuting sql command.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top