|
-
Jul 9th, 2004, 04:48 AM
#1
Thread Starter
New Member
No Returned data on SQLDataReader
I keep getting 'Invalid attempt to read when data is present' at line retObj.DatabaseID = DR.GetString(0)
I checked the connection str: works fine
I checked the SQL str: work fine
VB Code:
Public Class NetDatabase ...'properties hidden
Public Function GetDetails(ByVal DatabaseID As String) As NetDatabase
Dim retObj As New NetDatabase
Dim DR As SqlDataReader
Dim dataAcc As New DataAccess
Try
DR = dataAcc.GetData("SELECT * FROM [database] where DatabaseID ='" & DatabaseID & "'")
retObj.DatabaseID = DR.GetString(0)
retObj.DatabaseName = DR.GetString(1)
retObj.Domestic = DR.GetString(2)
retObj.Connectionstring = DR.GetString(3)
Return retObj
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
End Class
Private Class DataAccess
Dim mConnectionString As String = "SERVER=192.168.0.1;DATABASE=DEV_NETUSERDB;UID=sa;PWD=pwd;"
Private ReadOnly Property ConnectionString() As String
Get
Return mConnectionString
End Get
End Property
Public Function GetData(ByVal SQLstr As String) As SqlDataReader
Dim sqlDR As SqlDataReader
Dim DA As New DataAccess
Try
Dim sqlCn As New SqlConnection(DA.mConnectionString)
Dim sqlCmd As New SqlCommand(SQLstr, sqlCn)
sqlCn.Open() 'open connection with sql string
If UCase(Left(SQLstr, 6) = "SELECT") Then
sqlDR = sqlCmd.ExecuteReader
While sqlDR.Read()
sqlDR.Close() 'Why? Cos MSDN Says:you cannot retrieve output parameters until after you call Close.
Return sqlDR
End While
Else
sqlCmd.ExecuteNonQuery()
End If
sqlCmd.Connection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
End Class
Has someone out there have any idea why?
If life throws you a curve,
code a parabole.
-
Jul 9th, 2004, 08:12 AM
#2
I wonder how many charact
two things to try:
Insert a Dr.Read() in the line above where you attempt to grab the first value.
Also, and this is dependant on how you deal with no rows returned, you should have a If DR.HasRows statement...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|