CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Sep 2014
    Posts
    48

    Local variable cannot be referred to before it is declared - VS error

    Hello

    I have this code in my Visual Studio 2017 Web Forms project which is giving me a red underline error: Local variable 'cmdText' cannot be referred to before it is declared.

    Code:
    Protected Function Authenticate(strEmailValue As String, hashedValue As String) As Boolean
    
            'strEmailValue is the unknown email variable
            'hashedValue is the unknown password variable
            'strEmailTextBox is the ID of the email textbox field in my aspx file
            'passwordTextBox is the ID of the password textbox field in my aspx file
            'strEmail is the name of the email column in my MS Access database
    
            Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
                Dim cmd As New OleDbCommand(cmdText, connection)
    
                With cmd.Parameters
                    .Add("@strEmail", OleDbType.VarChar, 100).Value = strEmailValue
                    .Add("@hashed", OleDbType.VarChar, 100).Value = hashedValue
                End With
    
                Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
    
                connection.Open()
    
                Dim result As Integer = cmd.ExecuteScalar
    
                connection.Close()
    
                Return result > 0
    
            End Using
    
        End Function
    What should I be doing, please?

    Thank you.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Local variable cannot be referred to before it is declared - VS error

    You need to move your dim for cmdtext above the line where it is being refered to in the statement
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2014
    Posts
    48

    Re: Local variable cannot be referred to before it is declared - VS error

    Thanks, DataMiser for your reply.

    I cannot move
    Code:
    Dim cmd As New OleDbCommand(cmdText, connection)
    above

    Code:
    Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
    because I would then be using it before declaring it, so I have used it here:

    Code:
     Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
                With cmd.Parameters
                    .Add("@strEmail", OleDbType.VarChar, 100).Value = strEmailValue
                    .Add("@hashed", OleDbType.VarChar, 100).Value = hashedValue
                End With
    
                 Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
    
                Dim cmd As New OleDbCommand(cmdText, connection)
                connection.Open()
    Unfortunately, that gives me an error under the word 'cmd' on this line:

    Code:
    With cmd.Parameters
    with error being that it can't be used before it is declared.

    Thanks again.

    Steve

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Local variable cannot be referred to before it is declared - VS error

    Quote Originally Posted by SteveHi View Post
    Hello

    I have this code in my Visual Studio 2017 Web Forms project which is giving me a red underline error: Local variable 'cmdText' cannot be referred to before it is declared.

    Code:
    Protected Function Authenticate(strEmailValue As String, hashedValue As String) As Boolean
    
            'strEmailValue is the unknown email variable
            'hashedValue is the unknown password variable
            'strEmailTextBox is the ID of the email textbox field in my aspx file
            'passwordTextBox is the ID of the password textbox field in my aspx file
            'strEmail is the name of the email column in my MS Access database
    
            Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
                Dim cmd As New OleDbCommand(cmdText, connection)
    
                With cmd.Parameters
                    .Add("@strEmail", OleDbType.VarChar, 100).Value = strEmailValue
                    .Add("@hashed", OleDbType.VarChar, 100).Value = hashedValue
                End With
    
                Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
    
                connection.Open()
    
                Dim result As Integer = cmd.ExecuteScalar
    
                connection.Close()
    
                Return result > 0
    
            End Using
    
        End Function
    What should I be doing, please?

    Thank you.
    Try to move
    Code:
               Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
    up and place it before the line
    Code:
      Dim cmd As New OleDbCommand(cmdText, connection)
    
    Victor Nijegorodov

  5. #5
    Join Date
    Sep 2014
    Posts
    48

    Re: Local variable cannot be referred to before it is declared - VS error

    Hello VictorN

    Thanks for replying.

    That line is already above the second line:

    Code:
      Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
    
                'Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = '" & strEmailValue & "' AND hashed = '" & hashedValue & "'"
    
                Dim cmd As New OleDbCommand(cmdText, connection)
    Here is a screenshot with the cmd (at the top) error highlighted.

    Name:  yellow.jpg
Views: 386
Size:  30.2 KB

    Thanks again.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Local variable cannot be referred to before it is declared - VS error

    Your code should look like:
    Code:
    Protected Function Authenticate(strEmailValue As String, hashedValue As String) As Boolean
    
            'strEmailValue is the unknown email variable
            'hashedValue is the unknown password variable
            'strEmailTextBox is the ID of the email textbox field in my aspx file
            'passwordTextBox is the ID of the password textbox field in my aspx file
            'strEmail is the name of the email column in my MS Access database
    
            Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
                Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
                Dim cmd As New OleDbCommand(cmdText, connection)
    
                With cmd.Parameters
                    .Add("@strEmail", OleDbType.VarChar, 100).Value = strEmailValue
                    .Add("@hashed", OleDbType.VarChar, 100).Value = hashedValue
                End With
    
    
                connection.Open()
    
                Dim result As Integer = cmd.ExecuteScalar
    
                connection.Close()
    
                Return result > 0
    
            End Using
    
        End Function
    Victor Nijegorodov

  7. #7
    Join Date
    Sep 2014
    Posts
    48

    Re: Local variable cannot be referred to before it is declared - VS error

    Marvellous!

    Many thanks, Victor - error has now disappeared.

    I have tried to rate your post but the forum tells me to share it out more!

    Thanks again.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Local variable cannot be referred to before it is declared - VS error

    You are welcome!
    Victor Nijegorodov

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Local variable cannot be referred to before it is declared - VS error

    Your original code was
    Code:
                Dim cmd As New OleDbCommand(cmdText, connection)
    
                With cmd.Parameters
                    .Add("@strEmail", OleDbType.VarChar, 100).Value = strEmailValue
                    .Add("@hashed", OleDbType.VarChar, 100).Value = hashedValue
                End With
    
                Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = @strEmail AND hashed = @hashed"
    I had advised you to
    You need to move your dim for cmdtext above the line where it is being refered to in the statement
    Instead of moving the line up you moved a different line down and caused another problem.

    Always remember that code is executed from the top down. All dim statements must appear before the variable or object in question is used. In your original code the dim cmd line failed because you reference cmdText on that line but did not dim it until later. Then when you moved the dim for cmd down you where referencing cmd in your with block but it was not defined until later. If you would have moved the line I told you to it would have worked the first time
    Last edited by DataMiser; March 16th, 2019 at 06:51 PM.
    Always use [code][/code] tags when posting code.

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

Featured