Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 sqlreader has no data whenit should!

Author  Topic 

adjones1980
Starting Member

36 Posts

Posted - 2007-07-31 : 10:26:37
This is my code...

Dim strConnString As String = ConfigurationManager.ConnectionStrings _
("ConnectionString1").ConnectionString

Dim sqlConn As New SqlConnection(strConnString)
Dim DR As SqlDataReader
Try
Dim sqlCmd As New SqlCommand("SELECT Video FROM tbl_tmp_CP WHERE CP = " & Request.QueryString("CP").ToString, sqlConn)
sqlCmd.CommandType = CommandType.Text
sqlConn.Open()

DR = sqlCmd.ExecuteReader()
If DR.HasRows Then
If DR.GetValue(0) Is DBNull.Value Then ----->>> Error Here
Return "0"
Else
Return Format(CStr(DR.GetValue(0)), "#,###.00")
End If

Else
Return Nothing
End If

DR.Close()
sqlConn.Close()

Catch ex As Exception
If Not DR Is Nothing Then
If Not DR.IsClosed Then
DR.Close()
End If
End If

If Not sqlConn.State = ConnectionState.Closed Then
sqlConn.Close()
End If
Return ""

End Try


When I run the above code I get the error Invalid attempt to read when no data is present when I get to the point stated above.

I have tried the Select Statement in Query analyzer and I get...


Video
-----
0

The reader says that it has rows but I cannot access any of the data.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-31 : 10:28:38
i don't see your DR.Read()

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

adjones1980
Starting Member

36 Posts

Posted - 2007-07-31 : 10:34:45
I am such an idiot
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-31 : 10:42:57
it happens to the best of us

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-31 : 10:43:37
also if you're just getting one value use ExecuteScalar rather than executeReader

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page
   

- Advertisement -