"invalid attempt to read when no data is present"

  • Thread starter Thread starter Bruno Alexandre
  • Start date Start date
B

Bruno Alexandre

Hi guys...

I'm stuck!


I'm devoloping this program and everything was doing fine, until now!

This is what happends.


<my VB.NET 2.0 code>

Sub getUtenteIntoVariables(ByVal idUtente As String)

Dim conn As New SqlClient.SqlConnection(connString)
Dim cmd As New SqlClient.SqlCommand
Dim reader As SqlClient.SqlDataReader
Dim strSQL As String

Try

strSQL = "SELECT tnome FROM utiUtentes"

With cmd
.CommandText = strSQL
.CommandType = CommandType.Text
.Connection = conn
End With

conn.Open()
reader = cmd.ExecuteReader()

' Data is accessible through the DataReader object here.
Uten_Nome.Text = reader.Item("tnome").ToString

conn.Close()

Catch ex As Exception

MsgBox("Error: " & vbCrLf & ex.Message & vbCrLf & vbCrLf & "SQL Query: "
& vbCrLf & strSQL & vbCrLf & vbCrLf)

Finally

'Beep()

End Try

End Sub

</my VB.NET 2.0 code>


When it gets into this line Uten_Nome.Text = reader.Item("tnome").ToString
the program breaks and shows me this message:

"invalid attempt to read when no data is present"

I know that I have date in it, I can see with GetOrdinal("columnname")
method....

What am I doing wrong?
 
You have to use the Read method to read the first line.

As a side note GetOrdinal doesn't have anything to do with "data" but rather
"metadata" (it just describes the columns). You can use it even if you have
not read yet thr resultset or even if the result set is empty...

(also try to narrow down the post to appropriate groups...)
 
Back
Top