|
-
Nov 2nd, 2005, 11:32 AM
#1
Thread Starter
New Member
[RESOLVED] Problems with datareader
Hello,
I´m trying to make a LOGIN BOX using a datareader and a table with 4 columns:
- chvpassword
- txtlogin
- txtpassword
- chvtipoutilizador (in portuguese: type of user)
but i get problems in this line:
VB Code:
Me.chvtipoutilizador = ler.Item(4)
the error message says: "Invalid attempt to read when no data is present"
but i got that column filled! I dont knows what happens!
the whole code:
VB Code:
Imports System.Data.SqlClient
Public Class Login
Public chvtipoutilizador As Integer
Private Sub tbEntrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbEntrar.Click
login()
End Sub
Sub login()
Dim textoconexao As String = "server=(local);database=orca2005;integrated security=true"
Dim connection As New SqlConnection(textoconexao)
Dim comandoSQL As New Data.SqlClient.SqlCommand
Dim parametro As New SqlParameter
comandoSQL.CommandType = Data.CommandType.Text
comandoSQL.CommandText = "select * from tabPassword where txtlogin=@login and txtPassword=@Password"
comandoSQL.Connection = connection
Try
connection.Open()
parametro = comandoSQL.Parameters.Add("@login", Data.SqlDbType.NVarChar)
parametro.Direction = Data.ParameterDirection.Input
parametro.Value = Me.tbNome.Text
parametro = comandoSQL.Parameters.Add("@Password", Data.SqlDbType.NVarChar)
parametro.Direction = Data.ParameterDirection.Input
parametro.Value = Me.tbPassword.Text
If connection.State = Data.ConnectionState.Open Then
Dim ler As SqlDataReader
ler = comandoSQL.ExecuteReader
If ler.HasRows Then
MessageBox.Show("Seja Bem-vindo!")
Me.chvtipoutilizador = ler.Item(4)
tipo()
Dim entrar As New principal
entrar.Show()
Me.Close()
Else
MessageBox.Show("Erro de login!")
Me.tbNome.Clear()
Me.tbPassword.Clear()
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
comandoSQL.Dispose()
connection.Close()
End Try
End Sub
Sub tipo()
If chvtipoutilizador = 1 Then
principal.labellog.Text = "Administrador"
Else
principal.labellog.Text = "Utilizador"
End If
End Sub
End Class
-
Nov 2nd, 2005, 11:44 AM
#2
Frenzied Member
Re: Problems with datareader
Are you sure as it starts at 0 so col 4 is actually 3?
Herses some code from one of my readers
VB Code:
Do While objDatabase.SqlRdrLKAgents.Read
AgentName = objDatabase.SqlRdrLKAgents.GetString(0)
So it looks like your missing the Get part in yours.
Also
VB Code:
comandoSQL.CommandText = "select * from tabPassword where txtlogin=@login and txtPassword=@Password"
is very bad, if someone adds a new column in the database and your referencing in your apps by column number then your col references returned will go out of sync use the column names in your command text.
Last edited by FishGuy; Nov 2nd, 2005 at 11:49 AM.
-
Nov 2nd, 2005, 11:56 AM
#3
Re: Problems with datareader
You have to .Read from your reader before retrieving values.
-tg
-
Nov 2nd, 2005, 07:19 PM
#4
Re: Problems with datareader
Usually you use a While loop to get data from a DataReader:
VB Code:
While myReader.Read()
'Get values from current record here.
End While
The Read method advances to the next record and returns False when there are no records left. You must call it first to advance to the first record.
-
Nov 2nd, 2005, 07:25 PM
#5
Thread Starter
New Member
Re: Problems with datareader
 Originally Posted by techgnome
You have to .Read from your reader before retrieving values.
-tg
And how i do that?
-
Nov 2nd, 2005, 10:08 PM
#6
Re: Problems with datareader
Like this:
 Originally Posted by jmcilhinney
Usually you use a While loop to get data from a DataReader:
VB Code:
While myReader.Read()
'Get values from current record here.
End While
The Read method advances to the next record and returns False when there are no records left. You must call it first to advance to the first record.
-tg`
-
Nov 2nd, 2005, 10:15 PM
#7
Re: Problems with datareader
Now that's good advice, tg.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|