Wednesday, March 7, 2012

Invalid attempt to read when no data is present

I am using a standard dbreader type of loop in a query to retrieve data. I am running over what should be end of record set, every time.

I have altered my read procedures to use while dbreader.read() and if dbreader.read(), to attempt to avoid getting the error. Neither is stopping it.

While debugging it, as I get to the last item and actually get the error, if I check the dbreader status, it still indicates that it has rows.

Anyone have any ideas on how to get around this?

TIA, Tom

Can you post your code?
|||

Sure can. The below posted is the whole thing. The sqlcommand, connection, etc. should be irrelavant.

' get the data from the database and pass it back.

Function getTheData(ByVal cmd As String) As DataTable

' note that the passed cmd is the below sqlString

Dim sqlString As String = "SELECT CONVERT(char(20), Date, 107) AS Date, City, State, Company, Position, JobNumber, Row_Number() Over (ORDER BY JobNumber DESC) as Item FROM JobsDB "

' define the new datatable to hold our results

dt = New DataTable("Jobs")

' define the columns we will be saving

Dim dcIt As New DataColumn("Item", GetType(String))

Dim dcDt As New DataColumn("Date", GetType(String))

Dim dcCt As New DataColumn("City", GetType(String))

Dim dcSt As New DataColumn("State", GetType(String))

Dim dcJN As New DataColumn("JobNumber", GetType(Integer))

Dim dcKW As New DataColumn("KeyWords", GetType(String))

Dim dcPos As New DataColumn("Position", GetType(String))

Dim dcCo As New DataColumn("Company", GetType(String))

' add columns

dt.Columns.Add(dcIt)

dt.Columns.Add(dcDt)

dt.Columns.Add(dcCt)

dt.Columns.Add(dcSt)

dt.Columns.Add(dcJN)

dt.Columns.Add(dcKW)

dt.Columns.Add(dcPos)

dt.Columns.Add(dcCo)

' define datarow

Dim dr As DataRow

' build sql command info

sqlCmd = New SqlCommand

sqlCmd.Connection = sqlConn

sqlCmd.CommandType = CommandType.Text

sqlCmd.CommandText = sqlString

Try

sqlConn.Open()

dbReader = sqlCmd.ExecuteReader()

If dbReader.HasRows Then

While dbReader.Read() 'this can be changed to if dbreader.read(), no diff

' build the data table item from the database

dr = dt.NewRow()

dr("Item") = dbReader.Item("Item").ToString()

dr("Date") = dbReader.Item("Date").ToString()

dr("City") = dbReader.Item("City").ToString()

dr("State") = dbReader.Item("State").ToString()

dr("JobNumber") = dbReader.Item("JobNumber").ToString()

dr("Position") = dbReader.Item("Position").ToString()

dr("Company") = dbReader.Item("Company").ToString()

dr("KeyWords") = keywords

dt.Rows.Add(dr)

End While

Else

lblNoData.Visible = True

End If

Catch ex As Exception

Dim exMsg As String = Request.ServerVariables("Script_Name") + ", getTheData(cmd=" & sqlString & "): msg=" + ex.Message.ToString()

utils.writeApplicationLog(exMsg, System.Configuration.ConfigurationManager.AppSettings("UtilityDbName"))

Response.Redirect(ConfigurationManager.AppSettings("errorPage") & ConfigurationManager.AppSettings("errCatchAll") & "&return=" & Request.ServerVariables("Script_Host"))

End Try

Return dt.Copy

End Function

Thanks, Tom

|||The code worked for me using sample data.

Some thoughts: Make the declarations of sqlConn, sqlCmd, and dbReader local to the function instead of global. Add a Finally section that contains dbReader.Close, sqlConn.Close, and sqlConn.Dispose.

If that doesn't change anything, is it possible that there's something in your data that is causing the problem?

No comments:

Post a Comment