Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good evening guys, you are writing this code but it tells me that I have an ExecuteReader open.
Could you give me a hand
Thank you

What I have tried:

VB
        Dim namecontr As String
        Dim rdr3 As Object

        Dim cmd3 As New MySqlCommand("Select s01_name_contr  from s01_tpcontratti where id=" & rdr.Item(2), cnnmysql)
        rdr3 = cmd3.ExecuteReader
        rdr3 = cmd3.ToString   ' 03/12 ore 17:25 capire perche non mi funziona si blocca 29/12 non ancora sbloccato
        'cmd3.Dispose()

        '  rdr3.Read()



        If rdr3.HasRows = True Then
            namecontr = rdr3.Item(0).ToString
        End If

        Dim data As Date = CDate(rdr.Item(3).ToString)
        grid_contratto.Rows.Add(rdr(0).ToString, rdr(2).ToString, namecontr, "stocazzo", data.ToShortDateString)
        '   rdr3.close()
    Loop
    rdr.Close()

    'cnnmysql.Clone()

End Sub


EDIT - OPs code from "Solution 1" and Solution deleted
VB
Private Sub carica_grigliacontratti(id As String)
    '  Call connetti_mysql()
    ' cnnmysql.Open()

    If CInt("0" & id) = 0 Then Exit Sub
    Dim sql As String
    Dim rdr As Object
    ' Dim rdr2 As Object
    '  grspecialisti.Rows.Clear()


    sql = "Select * from a01_contratti where a01_cliente=" & id


    Dim cmd1 As New MySqlCommand(sql, cnnmysql)

    rdr = cmd1.ExecuteReader()
    cmd1.Dispose()


    If rdr.HasRows = False Then
        rdr.Close()

        Exit Sub
    End If
    sistema_grcerca()

    '    Dim perc As Long = 0
    '        Dim volte As Integer = 0w
    cnnmysql.Clone()
    grid_contratto.Rows.Clear()

    Do While rdr.Read

        Dim namecontr As String
        Dim rdr3 As Object

        Dim cmd3 As New MySqlCommand("Select s01_name_contr  from s01_tpcontratti where id=" & rdr.Item(2), cnnmysql)
        rdr3 = cmd3.ExecuteReader
        rdr3 = cmd3.ToString   ' 03/12 ore 17:25 capire perche non mi funziona si blocca 29/12 non ancora sbloccato
        'cmd3.Dispose()

        '  rdr3.Read()



        If rdr3.HasRows = True Then
            namecontr = rdr3.Item(0).ToString
        End If

        Dim data As Date = CDate(rdr.Item(3).ToString)
        grid_contratto.Rows.Add(rdr(0).ToString, rdr(2).ToString, namecontr, "stocazzo", data.ToShortDateString)
        '   rdr3.close()
    Loop
    rdr.Close()

    'cnnmysql.Clone()

End Sub
Posted
Updated 23-Jan-24 4:10am
v3
Comments
CHill60 23-Jan-24 8:53am    
You also have a problem with a risk of SQL Injection - use a parameterised query. DON'T concatenate strings to construct your SQL statement
CHill60 23-Jan-24 8:54am    
We will also need the rest of the Sub above the snippet you have provided
CHill60 23-Jan-24 10:09am    
By posting your code in a solution you have removed your question from the list of unanswered questions. I am going to post your code into your original post using the "Improve question" link, then I am going to delete your "Solution 1" - more people will see your post therefore more likely that you will get help

1 solution

When you call ExecuteReader, it "locks" the connection because the reader fetches a row's worth of information only when you call Read. To free the lock, you close the connection. The best way to handle this is to always create a connection object in a Using block[^], then inside that block you create a command in a second Using block, and a reader in a third inside that:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using cmd As New SqlCommand("SELECT iD, description FROM myTable", con)
		Using reader As SqlDataReader = cmd.ExecuteReader()
			While reader.Read()
				Dim myId As Integer = CInt(reader("iD"))
				Dim myDesc As String = DirectCast(reader("description"), String)
				Console.WriteLine("ID: {0}" & vbLf & "    {1}", myId, myDesc)
			End While
		End Using
	End Using
End Using
When your code exits the Using block by any means (Break, Return, or Exception for example) the object created within it is automatically closed and disposed for you and you never have a problem with unclosed readers or connections.
 
Share this answer
 
v3
Comments
PIEBALDconsult 23-Jan-24 10:51am    
We advanced users never do that. That's for newbs. ;)
OriginalGriff 23-Jan-24 11:32am    
Yeah, I just like getting them to type more ... :laugh:
Andre Oosthuizen 24-Jan-24 14:55pm    
I love typing more, makes me feel like a newb :) Had a good laugh, thanks!
Member 16188004 30-Jan-24 12:36pm    
ok, but I have to go and select the id in another table
OriginalGriff 30-Jan-24 12:52pm    
That makes no sense to me: you need to explain in more detail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900