Results 1 to 7 of 7

Thread: [RESOLVED] Problems with datareader

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Resolved [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:
    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

  2. #2
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Smile 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:
    1. Do While objDatabase.SqlRdrLKAgents.Read
    2.                 AgentName = objDatabase.SqlRdrLKAgents.GetString(0)
    So it looks like your missing the Get part in yours.

    Also
    VB Code:
    1. 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.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Problems with datareader

    You have to .Read from your reader before retrieving values.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problems with datareader

    Usually you use a While loop to get data from a DataReader:
    VB Code:
    1. While myReader.Read()
    2.     'Get values from current record here.
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: Problems with datareader

    Quote Originally Posted by techgnome
    You have to .Read from your reader before retrieving values.

    -tg
    And how i do that?

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Problems with datareader

    Like this:

    Quote Originally Posted by jmcilhinney
    Usually you use a While loop to get data from a DataReader:
    VB Code:
    1. While myReader.Read()
    2.     'Get values from current record here.
    3. 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`
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problems with datareader

    Now that's good advice, tg.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width