Results 1 to 2 of 2

Thread: No Returned data on SQLDataReader

  1. #1

    Thread Starter
    New Member callisto's Avatar
    Join Date
    Mar 2003
    Posts
    14

    Unhappy 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:
    1. Public Class NetDatabase    ...'properties hidden            
    2. Public Function GetDetails(ByVal DatabaseID As String) As NetDatabase              
    3. Dim retObj As New NetDatabase              
    4. Dim DR As SqlDataReader              
    5. Dim dataAcc As New DataAccess            
    6.  Try                  
    7. DR = dataAcc.GetData("SELECT * FROM [database] where DatabaseID ='" & DatabaseID & "'")                  
    8.  retObj.DatabaseID = DR.GetString(0)                
    9.  retObj.DatabaseName = DR.GetString(1)                
    10.  retObj.Domestic = DR.GetString(2)                
    11.  retObj.Connectionstring = DR.GetString(3)                  
    12.  Return retObj              
    13. Catch ex As Exception                
    14.  MsgBox(ex.ToString)            
    15.  End Try        
    16.  End Function        
    17. End Class        
    18.  
    19. Private Class DataAccess                    
    20. Dim mConnectionString As String = "SERVER=192.168.0.1;DATABASE=DEV_NETUSERDB;UID=sa;PWD=pwd;"          
    21. Private ReadOnly Property ConnectionString() As String              
    22. Get                  
    23. Return mConnectionString              
    24. End Get          
    25. End Property    
    26.        
    27. Public Function GetData(ByVal SQLstr As String) As SqlDataReader              
    28. Dim sqlDR As SqlDataReader              
    29. Dim DA As New DataAccess              
    30. Try                    
    31. Dim sqlCn As New SqlConnection(DA.mConnectionString)                  
    32. Dim sqlCmd As New SqlCommand(SQLstr, sqlCn)
    33.   sqlCn.Open() 'open connection with sql string                    
    34. If UCase(Left(SQLstr, 6) = "SELECT") Then                    
    35.  sqlDR = sqlCmd.ExecuteReader                    
    36.  While sqlDR.Read()                          
    37. sqlDR.Close()  'Why? Cos MSDN Says:you cannot retrieve output parameters until after you call Close.
    38. Return sqlDR                    
    39. End While                  
    40. Else                      
    41. sqlCmd.ExecuteNonQuery()                    
    42. End If                    
    43. sqlCmd.Connection.Close()                                
    44. Catch ex As Exception                
    45. MsgBox(ex.ToString)              
    46. End Try        
    47. End Function      
    48. End Class


    Has someone out there have any idea why?
    If life throws you a curve,
    code a parabole.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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
  •  



Click Here to Expand Forum to Full Width