3 Replies - 343 Views - Last Post: 18 January 2012 - 06:41 AM Rate Topic: -----

#1 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

SQL error

Posted 17 January 2012 - 09:43 PM

Howdy Folks. Everytime I run this tid bit of code it crashes, and the error states there is no data present for retrieving. This is wrong, one there is lots of data to retrieve, and two the code that adds that data before this function runs is working since the database is being filled with values, and I can see that happen. Any ideas on what's going on? It was working before and now suddenly it keeps crashing and saying there are no values present.

    Public Shared Function GetEndOfPayPeriod() As String
        Try
            Dim SQLConn As New SqlConnection(SQLConnectionString)
            Dim SQLStatement As String = "SELECT EndOfPayPeriod FROM PayPeriod"
            Dim SQLComm As New SqlCommand(SQLStatement, SQLConn)

            SQLConn.Open()

            Dim sqlreader As SqlDataReader = SQLComm.ExecuteReader

            If sqlreader.HasRows = True Then
                Return sqlreader.Item(0).ToString
            Else
                Return "9999999"
            End If

            SQLComm.Dispose()
            SQLConn.Close()
            SQLConn.Dispose()

        Catch ex As Exception
            Global.Admin.GlobalErrorMessage = ex.Message & " GetEndOfPayPeriod"
        End Try
    End Function



It's cracking my skull here trying to figure this one out. I'm this close to finishing this project and every time I get to be about done some unexplainable error shows up somewhere I tell ya.

Is This A Good Question/Topic? 0
  • +

Replies To: SQL error

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: SQL error

Posted 18 January 2012 - 05:32 AM

Your problem is because you didnt read from your reader, so you cant retrieve any data before reading a row!
Have this change in your code and it will works fine:
If sqlreader.HasRows = True Then 'First check if any row is there
   sqlreader.Read() 'read the next record/row before trying to retrieve it
   Return sqlreader.Item(0).ToString 'then retrieve it
End If

Was This Post Helpful? 1
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

Re: SQL error

Posted 18 January 2012 - 06:29 AM

Ha! Thanks smohd knew I had been looking at it too long. I had tried it without at some point and with a little luck it worked so I shrugged it off.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: SQL error

Posted 18 January 2012 - 06:41 AM

Ok glad you get it working....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1