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:
  1. 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:
  1. Imports System.Data.SqlClient
  2. Public Class Login
  3.     Public chvtipoutilizador As Integer
  4.  
  5.     Private Sub tbEntrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbEntrar.Click
  6.         login()
  7.  
  8.     End Sub
  9.     Sub login()
  10.  
  11.  
  12.         Dim textoconexao As String = "server=(local);database=orca2005;integrated security=true"
  13.         Dim connection As New SqlConnection(textoconexao)
  14.         Dim comandoSQL As New Data.SqlClient.SqlCommand
  15.         Dim parametro As New SqlParameter
  16.         comandoSQL.CommandType = Data.CommandType.Text
  17.         comandoSQL.CommandText = "select * from tabPassword where txtlogin=@login and txtPassword=@Password"
  18.         comandoSQL.Connection = connection
  19.         Try
  20.             connection.Open()
  21.             parametro = comandoSQL.Parameters.Add("@login", Data.SqlDbType.NVarChar)
  22.             parametro.Direction = Data.ParameterDirection.Input
  23.             parametro.Value = Me.tbNome.Text
  24.             parametro = comandoSQL.Parameters.Add("@Password", Data.SqlDbType.NVarChar)
  25.             parametro.Direction = Data.ParameterDirection.Input
  26.             parametro.Value = Me.tbPassword.Text
  27.             If connection.State = Data.ConnectionState.Open Then
  28.                 Dim ler As SqlDataReader
  29.                 ler = comandoSQL.ExecuteReader
  30.  
  31.                 If ler.HasRows Then
  32.                     MessageBox.Show("Seja Bem-vindo!")
  33.                     Me.chvtipoutilizador = ler.Item(4)
  34.                     tipo()
  35.  
  36.                     Dim entrar As New principal
  37.                     entrar.Show()
  38.                     Me.Close()
  39.                 Else
  40.                     MessageBox.Show("Erro de login!")
  41.                     Me.tbNome.Clear()
  42.                     Me.tbPassword.Clear()
  43.  
  44.                 End If
  45.             End If
  46.         Catch ex As Exception
  47.             MessageBox.Show(ex.Message)
  48.         Finally
  49.             comandoSQL.Dispose()
  50.             connection.Close()
  51.         End Try
  52.  
  53.     End Sub
  54.     Sub tipo()
  55.         If chvtipoutilizador = 1 Then
  56.             principal.labellog.Text = "Administrador"
  57.         Else
  58.             principal.labellog.Text = "Utilizador"
  59.         End If
  60.     End Sub
  61.  
  62.  
  63. End Class