-
March 10th, 2019, 10:09 AM
#1
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.
-
March 11th, 2019, 10:54 PM
#2
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.
-
March 12th, 2019, 01:32 AM
#3
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
-
March 12th, 2019, 02:21 AM
#4
Re: Local variable cannot be referred to before it is declared - VS error
Originally Posted by SteveHi
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
-
March 12th, 2019, 06:56 AM
#5
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.
Thanks again.
-
March 12th, 2019, 07:15 AM
#6
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
-
March 12th, 2019, 08:07 AM
#7
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.
-
March 12th, 2019, 08:46 AM
#8
Re: Local variable cannot be referred to before it is declared - VS error
Victor Nijegorodov
-
March 16th, 2019, 06:46 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|