Sqldatareader Not Closed?

Asked

Viewed 63 times

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 variable dr (Sqldatareader) was not closed. At the end of each procedure, always close the variable connection dr. Someone can help me?

  • only to ensure, after dr.Close() puts dr = Nothing

  • Yeah. It doesn’t work :/

  • Strange... the mistake happens in both methods? (Get_Disciplinas() and Get_Turma()) ?

  • No. Only in Get_turma(). I have another colleague with the same mistake..

  • after the While dr.Read(), tries to remove all code inside, and leaves only one message box displaying this content: dr.Item(0).ToString works like this?

  • as in the comments I can not leave code formatted, with reply with a previous solution... I will change as you are giving the feedback

  • Ok. It already works if you remove all the code inside the while from the Get_class().

  • But if you put this code: F_top.Cb_class.Items.Add(dr.Item(0)) No longer works again.

  • see my answer

Show 4 more comments

1 answer

0

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

First... change the method in question so it stays that way:

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

Tries this amendment

  • Thank you. When I can test the code :) (tomorrow)

  • The "get_Turma()" function already works. What does not work now is get_disciplinas(). I updated the question to the current state.

  • An unhandled Exception of type 'System.Invalidoperationexception' occurred in System.Data.dll Additional information: The Connectionstring property was not initialized.

  • I have an idea that the problem is that I do this: 'Const Cs As String = "Data Source=D01PC1 SQLEXPRESS;Initial Catalog=Uninvite;Integrated Security=True"' 'Public con As New Sqlconnection(Cs)' Only once globally instead of locally doing in each procedure.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.