SqlDataReader Not Closed?

0
Imports System.Data.SqlClient

Public Class F_Contentor

Dim F_Topo As New Form2()
Dim F_Principal As New Form3()

Dim Prof_Ses_Nome As String
Dim Prof_Ses_Id As Integer

Const cs As String = "Data Source=D01PC1\SQLEXPRESS;Initial Catalog=Desinovar;Integrated Security=True"
Public con As New SqlConnection(cs)


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Prof_Ses_Nome = Login.prof_Ses_Nome.ToString()
    Prof_Ses_Id = Login.prof_Ses_ID
    Login.Close()
    F_Topo.TopLevel = False
    F_Principal.TopLevel = False
    Me.Painel_Topo.Controls.Add(F_Topo)
    Me.Painel_Main.Controls.Add(F_Principal)
    F_Topo.Show()
    F_Principal.Show()

    F_Topo.CB_Prof.Items.Add(Prof_Ses_Nome)
    F_Topo.CB_Prof.SelectedIndex = 0


End Sub

Public Sub Get_Turma()
    F_Topo.CB_Turma.Items.Clear()

    Using con
        Dim sql As String = "select Turma.Nome from Turma,Professores_Turma,Professores where Turma.Id_Turma = Professores_Turma.Id_Turma and Professores_Turma.Id_Professor = Professores.Id_Professor and Professores.Id_Professor ='" & Prof_Ses_Id & "'"
        Dim sqlCom As New SqlCommand(sql, con)
        con.Open()

        Dim dr As SqlDataReader = sqlcom.ExecuteReader()

        If dr.HasRows Then
            While dr.Read()
                F_Topo.CB_Turma.Items.Add(dr.Item(0))
            End While
        Else
            ' Aqui faça o que quiser caso não tenha linha '
        End If

        dr.Close()
    End Using
End Sub

Public Sub Get_Disciplinas()
    F_Topo.CB_Disciplina.Items.Clear()

    Using con
        Dim sql As String = "select Disciplinas.Nome from Disciplinas,Turma,Professores_Turma,Professores where Turma.Id_Turma = Professores_Turma.Id_Turma and Professores_Turma.Id_Professor = Professores.Id_Professor and Professores_Turma.Id_Disciplina = Disciplinas.Id_Disciplina  and Professores.Id_Professor ='" & Login.prof_Ses_ID & "' and Turma.Nome = '" & F_Topo.CB_Turma.SelectedItem & "'"
        Dim sqlCom As New SqlCommand(sql, con)
        con.Open()

        Dim dr As SqlDataReader = sqlCom.ExecuteReader()

        If dr.HasRows Then
            While dr.Read()
                F_Topo.CB_Disciplina.Items.Add(dr.Item(0))
            End While
        Else
            ' Aqui faça o que quiser caso não tenha linha '
        End If

        dr.Close()
    End Using

End Sub
End Class

I have this code and when I run it, it tells me that the dr (SqlDataReader) variable was not closed. At the end of each procedure, I always close the connection of the dr variable. Can Someone Help Me?

    
asked by anonymous 21.01.2016 / 10:44

1 answer

0

As in the comments I can not leave code formatted, with answer with a previous solution ... I will change as you give the feedback.

First ... change the method in question to look like this:

Public Sub Get_Turma()
    F_Topo.CB_Turma.Items.Clear()

    using con
        Dim sql As String = "select Turma.Nome from Turma,Professores_Turma,Professores where Turma.Id_Turma = Professores_Turma.Id_Turma and Professores_Turma.Id_Professor = Professores.Id_Professor and Professores.Id_Professor ='" & Prof_Ses_Id & "'"
        Dim sqlCom As new SqlCommand(sql, con)
        con.Open()

        Dim dr As SqlDataReader = sqlcom.ExecuteReader()

        If dr.HasRows Then
            While dr.Read()
                F_Topo.CB_Turma.Items.Add(dr.Item(0))
            End While
        Else
            ' Aqui faça o que quiser caso não tenha linha '
        End If

        dr.Close()
    End Using
End Sub

Try this change

    
21.01.2016 / 11:01