Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio 2015 & .NET 4.6
  4. Error: Variable 'conn' hides a variable in an enclosing block.

Error: Variable 'conn' hides a variable in an enclosing block.

Scheduled Pinned Locked Moved Visual Studio 2015 & .NET 4.6
csharpasp-netdatabasevisual-studiocom
8 Posts 3 Posters 23 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 8761667
    wrote on last edited by
    #1

    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://mySite.com/my_image.jpg/)](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)
    
    L 1 Reply Last reply
    0
    • M Member 8761667

      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://mySite.com/my_image.jpg/)](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)
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • L Lost User

        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

        M Offline
        M Offline
        Member 8761667
        wrote on last edited by
        #3

        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 OleDbDataAdapter

        I just get: Error 1:'conn' is not declared. It may be inaccessible due to its protection level. Thanks again for your help.

        Richard Andrew x64R 1 Reply Last reply
        0
        • M Member 8761667

          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 OleDbDataAdapter

          I just get: Error 1:'conn' is not declared. It may be inaccessible due to its protection level. Thanks again for your help.

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          One thing you're doing wrong is that you have conn declared inside a Using block, but the Using block ends before your Try block. When the Using block ends, it disposes the connection, so you cannot go on to use the connection inside the Try block.

          The difficult we do right away... ...the impossible takes slightly longer.

          M 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            One thing you're doing wrong is that you have conn declared inside a Using block, but the Using block ends before your Try block. When the Using block ends, it disposes the connection, so you cannot go on to use the connection inside the Try block.

            The difficult we do right away... ...the impossible takes slightly longer.

            M Offline
            M Offline
            Member 8761667
            wrote on last edited by
            #5

            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 = mailAuthenticaion

            SMTPMail.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=")

            Richard Andrew x64R 1 Reply Last reply
            0
            • M Member 8761667

              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 = mailAuthenticaion

              SMTPMail.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=")

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              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.

              M 1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                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.

                M Offline
                M Offline
                Member 8761667
                wrote on last edited by
                #7

                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.

                Richard Andrew x64R 1 Reply Last reply
                0
                • M Member 8761667

                  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.

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #8

                  Glad I could help. :)

                  The difficult we do right away... ...the impossible takes slightly longer.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups