Error: Variable 'conn' hides a variable in an enclosing block.
-
Hello With the error above, I understand that Visual Studio is telling me that 'conn' needs to be renamed. This is my code:
Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click
Dim cmd As OleDbCommand Dim dt As DataTable **Dim conn As New OleDbConnection 'Dim conn1 As New OleDbConnection** Dim adp As OleDbDataAdapter Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString) If conn.State = ConnectionState.Closed Then conn.Open() End If Dim SQL As String = "SELECT strEmail FROM university (strEmail) WHERE (strEmail=@strEmail)" cmd.Parameters.AddWithValue("@strEmail", UserEmail.Text) cmd.ExecuteNonQuery() conn.Close() End Using Try 'In OleDbDataAdapter, execute the SQL and check that the user's email corresponds to that in the MDB adp = New OleDbDataAdapter("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn) 'Pass email parameter named email from the value of Mail.Sent.Text cmd.Parameters.AddWithValue("@strEmail", UserEmail) dt = New DataTable() adp.Fill(dt) If dt.Rows.Count = 0 Then ErrorMail.Text = "Please enter a valid email address" Return Else Dim code As String code = Guid.NewGuid().ToString() ' and am updating the code column of the table with this value. i mean inside the code column i'll store the value ' that was inside the code variable cmd = New OleDbCommand("UPDATE university SET code=@code WHERE strEmail=@strEmail", conn) cmd.Parameters.AddWithValue("@code", code) cmd.Parameters.AddWithValue("@email", UserEmail.Text) ' here i am difinning a StringBuilder class named sbody Dim sbody As New StringBuilder() 'Send image as logo with the path http://whatever.com sbody.Append("[](http://whatever.com)
")
'Send link to user's email with email and code using querystring sbody.Append("[](http://usingasp.net/reset_pwd.aspx?e</x-turndown)
-
Hello With the error above, I understand that Visual Studio is telling me that 'conn' needs to be renamed. This is my code:
Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click
Dim cmd As OleDbCommand Dim dt As DataTable **Dim conn As New OleDbConnection 'Dim conn1 As New OleDbConnection** Dim adp As OleDbDataAdapter Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString) If conn.State = ConnectionState.Closed Then conn.Open() End If Dim SQL As String = "SELECT strEmail FROM university (strEmail) WHERE (strEmail=@strEmail)" cmd.Parameters.AddWithValue("@strEmail", UserEmail.Text) cmd.ExecuteNonQuery() conn.Close() End Using Try 'In OleDbDataAdapter, execute the SQL and check that the user's email corresponds to that in the MDB adp = New OleDbDataAdapter("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn) 'Pass email parameter named email from the value of Mail.Sent.Text cmd.Parameters.AddWithValue("@strEmail", UserEmail) dt = New DataTable() adp.Fill(dt) If dt.Rows.Count = 0 Then ErrorMail.Text = "Please enter a valid email address" Return Else Dim code As String code = Guid.NewGuid().ToString() ' and am updating the code column of the table with this value. i mean inside the code column i'll store the value ' that was inside the code variable cmd = New OleDbCommand("UPDATE university SET code=@code WHERE strEmail=@strEmail", conn) cmd.Parameters.AddWithValue("@code", code) cmd.Parameters.AddWithValue("@email", UserEmail.Text) ' here i am difinning a StringBuilder class named sbody Dim sbody As New StringBuilder() 'Send image as logo with the path http://whatever.com sbody.Append("[](http://whatever.com)
")
'Send link to user's email with email and code using querystring sbody.Append("[](http://usingasp.net/reset_pwd.aspx?e</x-turndown)
-
When you create a variable in a
using
block you should not have it dimensioned outside. Just remove the line:Dim conn As New OleDbConnection
Hello Richard Many thanks for your reply. When I do that, as follows:
Dim cmd As OleDbCommand
Dim dt As DataTable
'Dim conn As New OleDbConnection
'Dim conn1 As New OleDbConnection
Dim adp As OleDbDataAdapterI just get: Error 1:'conn' is not declared. It may be inaccessible due to its protection level. Thanks again for your help.
-
Hello Richard Many thanks for your reply. When I do that, as follows:
Dim cmd As OleDbCommand
Dim dt As DataTable
'Dim conn As New OleDbConnection
'Dim conn1 As New OleDbConnection
Dim adp As OleDbDataAdapterI just get: Error 1:'conn' is not declared. It may be inaccessible due to its protection level. Thanks again for your help.
One thing you're doing wrong is that you have
conn
declared inside aUsing
block, but theUsing
block ends before yourTry
block. When theUsing
block ends, it disposes the connection, so you cannot go on to use the connection inside theTry
block.The difficult we do right away... ...the impossible takes slightly longer.
-
One thing you're doing wrong is that you have
conn
declared inside aUsing
block, but theUsing
block ends before yourTry
block. When theUsing
block ends, it disposes the connection, so you cannot go on to use the connection inside theTry
block.The difficult we do right away... ...the impossible takes slightly longer.
I have separated 'Using' from 'Try' now - thank you.
Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click
conn = New OleDbConnection()
conn = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
conn.Open()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
adp = New OleDbDataAdapter("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn)
cmd.Parameters.AddWithValue("@strEmail", Request.QueryString("UserEmail").ToString())
cmd.ExecuteNonQuery()
conn.Close()
End Using
Dim sbody As New StringBuilder()
sbody.Append("[")
Try
Dim SMTPserver As New System.Net.Mail.SmtpClient("smtp.mail.server", 25)
Dim SMTPMail As New System.Net.Mail.MailMessage("LostPwd", "strEmail")
Dim mailAuthenticaion As New System.Net.NetworkCredential("info@mySite.com ", "SMTP server password")
SMTPserver.EnableSsl = True
SMTPserver.Credentials = mailAuthenticaionSMTPMail.To.Add("LostPwd.Text".ToString())
SMTPMail.CC.Add("")
SMTPMail.Bcc.Add("Webmaster@mySite.com")
SMTPMail.From = New MailAddress("info@mySite.net")
SMTPMail.Subject = "Please reset your password"
SMTPMail.Body = sbody.ToString()
SMTPMail.IsBodyHtml = True
SMTPserver.Send(SMTPMail)cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
LostPwd.Text = "Password reset link sent"
Catch ex As Exception
LostPwd.Text = "" 'Error message here
Finally
conn.Close() 'close the database
End Try
End Sub](http://usingasp.net/reset_pwd.aspx?email=")
-
I have separated 'Using' from 'Try' now - thank you.
Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click
conn = New OleDbConnection()
conn = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
conn.Open()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
adp = New OleDbDataAdapter("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn)
cmd.Parameters.AddWithValue("@strEmail", Request.QueryString("UserEmail").ToString())
cmd.ExecuteNonQuery()
conn.Close()
End Using
Dim sbody As New StringBuilder()
sbody.Append("[")
Try
Dim SMTPserver As New System.Net.Mail.SmtpClient("smtp.mail.server", 25)
Dim SMTPMail As New System.Net.Mail.MailMessage("LostPwd", "strEmail")
Dim mailAuthenticaion As New System.Net.NetworkCredential("info@mySite.com ", "SMTP server password")
SMTPserver.EnableSsl = True
SMTPserver.Credentials = mailAuthenticaionSMTPMail.To.Add("LostPwd.Text".ToString())
SMTPMail.CC.Add("")
SMTPMail.Bcc.Add("Webmaster@mySite.com")
SMTPMail.From = New MailAddress("info@mySite.net")
SMTPMail.Subject = "Please reset your password"
SMTPMail.Body = sbody.ToString()
SMTPMail.IsBodyHtml = True
SMTPserver.Send(SMTPMail)cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
LostPwd.Text = "Password reset link sent"
Catch ex As Exception
LostPwd.Text = "" 'Error message here
Finally
conn.Close() 'close the database
End Try
End Sub](http://usingasp.net/reset_pwd.aspx?email=")
Now you're instantiating conn twice! Once before the Using block, and then again inside the Using block. And you're still ending the Using block before the Try block, so the connection is being closed and disposed before you get to the heart of the code.
The difficult we do right away... ...the impossible takes slightly longer.
-
Now you're instantiating conn twice! Once before the Using block, and then again inside the Using block. And you're still ending the Using block before the Try block, so the connection is being closed and disposed before you get to the heart of the code.
The difficult we do right away... ...the impossible takes slightly longer.
You would think that would generate an error, wouldn't you? I have now removed
If conn.State = ConnectionState.Closed Then
conn.Open()End If
from inside 'Using' and I have moved 'End Using' right to the bottom
Finally
conn.Close() 'close the database End Try End Using End Sub
End Class
so it encompasses both the database part of the code and the SMTP part. I had better read up on 'Using' - thanks for pointing it out.
-
You would think that would generate an error, wouldn't you? I have now removed
If conn.State = ConnectionState.Closed Then
conn.Open()End If
from inside 'Using' and I have moved 'End Using' right to the bottom
Finally
conn.Close() 'close the database End Try End Using End Sub
End Class
so it encompasses both the database part of the code and the SMTP part. I had better read up on 'Using' - thanks for pointing it out.
Glad I could help. :)
The difficult we do right away... ...the impossible takes slightly longer.