|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
"invalid attempt to read when no data is present"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? -- Bruno Alexandre (Sintra, PORTUGAL) 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...) -- Show quotePatrice "Bruno Alexandre" <bruno.n***@filtrarte.com> a écrit dans le message de news:OgUEm9T6FHA.736@TK2MSFTNGP10.phx.gbl... > 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? > > > > > -- > Bruno Alexandre > (Sintra, PORTUGAL) > > > > > thank you.
-- Show quoteBruno Alexandre (Sintra, PORTUGAL) "Patrice" <nob***@nowhere.com> escreveu na mensagem news:uKShVIU6FHA.2176@TK2MSFTNGP14.phx.gbl... > 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...) > > -- > Patrice > > "Bruno Alexandre" <bruno.n***@filtrarte.com> a écrit dans le message de > news:OgUEm9T6FHA.736@TK2MSFTNGP10.phx.gbl... >> 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? >> >> >> >> >> -- >> Bruno Alexandre >> (Sintra, PORTUGAL) >> >> >> >> >> > > |
|||||||||||||||||||||||