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




Reply With Quote