Results 1 to 21 of 21

Thread: garbage collection error, give me an example here that is correct for this form

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    garbage collection error, give me an example here that is correct for this form

    I tried .dispose but it does not pop up as available for the connection string, I figure that means something is not correct.

    And how about closing a form and how do you set it to collect garbage?
    as in you do unload me, in vb6 you can say set frmname = Nothing


    Code:
    Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
    		
    		If SQLServerConn.State = 1 Then SQLServerConn.Close()
    		'UPGRADE_NOTE: Object SQLServerConn may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
    
            SQLServerConn.Dispose()
            SQLServerConn = Nothing
    
    		frmLogon.CancelOpener = True
    		
    		Me.Close()
    		
    	End Sub

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,547

    Re: garbage collection error, give me an example here that is correct for this form

    You don't need to call close if you are also calling Dispose.

    Also no need to set the variable to nothing either.

    Easiest way is to use https://learn.microsoft.com/en-us/do...sing-statement to handle the disposing of a resource.

    For a form just calling close is enough, no need to set anything to nothing here.

    Did you try to dispose of the connection or the connection string?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by PlausiblyDamp View Post
    You don't need to call close if you are also calling Dispose.

    Also no need to set the variable to nothing either.

    Easiest way is to use https://learn.microsoft.com/en-us/do...sing-statement to handle the disposing of a resource.

    For a form just calling close is enough, no need to set anything to nothing here.

    Did you try to dispose of the connection or the connection string?
    Well, I close the connection to the database.
    The connection string just has the info to connect to the database as a string.

    it did not like this line, SQLServerConn = Nothing
    So I thought to add .dispose on the end.
    I have not run it as the project starup form is not this form.
    Are you saying just don't do anything as in don't set anything to nothing?

    Also can vs2008 run without full compile like vb6 can?
    That way it will ignore syntax errors until it loads a form.
    If it can where is it it found in the IDE?

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,547

    Re: garbage collection error, give me an example here that is correct for this form

    Normally no need to set things to nothing under .net, just let the garbage collector do it's thing.

    There isn't any option to run without doing a full compile.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by PlausiblyDamp View Post
    Normally no need to set things to nothing under .net, just let the garbage collector do it's thing.

    There isn't any option to run without doing a full compile.
    That then makes it harder to program
    If it worked like vb6, it was easier, as in you can fix a form and it's code without fixing the entire project at once.

    As in why did MS make it more difficult.

    Maybe one way to do it is detach forms and codes and add them one at a time, except it likely will fail if the code say to load a form or a sub or a function on a another form, have to comment out the links to those forms
    Last edited by sdowney1; Apr 7th, 2024 at 02:40 PM.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    How about this sub issues
    see in vb6 restore brought a minimized form back up

    How would you do this in vs8?
    and why did vs2008 converter now call it a public sub?

    Code:
    Public Sub exit_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles exit_Renamed.Click
    		Dim vbrestore As Object
    		
    		Me.Close()
    		'UPGRADE_NOTE: Object frmData may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
    		Me = Nothing
    		frmLogon.Show()
    		'UPGRADE_WARNING: Couldn't resolve default property of object vbrestore. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
    		If frmLogon.WindowState = System.Windows.Forms.FormWindowState.Minimized Then frmLogon.WindowState = vbrestore
    		
    	End Sub
    working vb6 code for same sub is this here

    Code:
    Private Sub exit_Click()
      
       Unload Me
       Set frmData = Nothing
       frmLogon.Show
       If frmLogon.WindowState = vbMinimized Then frmLogon.WindowState = vbrestore
    
    End Sub

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    I would like to see working examples of doing the same things as are happening in vb6.
    So if anyone can share that, it will be helpful.
    I figure the solutions are really simple?

  8. #8
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,547

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by sdowney1 View Post
    How about this sub issues
    see in vb6 restore brought a minimized form back up

    How would you do this in vs8?
    and why did vs2008 converter now call it a public sub?

    Code:
    Public Sub exit_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles exit_Renamed.Click
    		Dim vbrestore As Object
    		
    		Me.Close()
    		'UPGRADE_NOTE: Object frmData may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
    		Me = Nothing
    		frmLogon.Show()
    		'UPGRADE_WARNING: Couldn't resolve default property of object vbrestore. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
    		If frmLogon.WindowState = System.Windows.Forms.FormWindowState.Minimized Then frmLogon.WindowState = vbrestore
    		
    	End Sub
    working vb6 code for same sub is this here

    Code:
    Private Sub exit_Click()
      
       Unload Me
       Set frmData = Nothing
       frmLogon.Show
       If frmLogon.WindowState = vbMinimized Then frmLogon.WindowState = vbrestore
    
    End Sub
    Is vbrestore a valid window state in vb6? IIRC the options were vbNormal, vbMaximized, and vbMinimized - so no wonder the upgrade tool struggled with vbrestore.

    Did your vb6 application use Option Explicit?

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,621

    Re: garbage collection error, give me an example here that is correct for this form

    If you use an sqlconnection the use the USING keyword . That will close and dispose the connection automatically so you don't have to worry about closing the connection.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,157

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by sapator View Post
    If you use an sqlconnection the use the USING keyword . That will close and dispose the connection automatically so you don't have to worry about closing the connection.
    It's probably even more severe than that. That would be right for .NET, but since this is a conversion from VB6, you have to consider the very different approach that VB6 and VB.NET take to databases. Even if the VB6 was using ADO, there has been a change.

    The whole point of database access in .NET can be thought of as, "get in, get your data, get out". VB6 was more focused on keeping a connection to a database. It was reasonably common in VB6 to open a connection in a form, and either hold it for the life of the form, or to pass it around for other forms. If you were doing something like that, then the ADO.NET approach will be quite a bit different.

    It is very rare to ever keep a connection open beyond a function in ADO.NET. I do know of a case, but usually it looks something like this:
    Code:
    Private Function SomeFunction()
     Using cn As New Connection(yourConnectionString)
       'Do stuff with the connection
     End Using
    End Function
    The Using block will properly clean up the connection, even if an exception is thrown, so you can have an exception handler either inside the Using block, around the outside of the using block, or both, and the Using block will clean things up correctly, with no need to close the connection.

    Of course, this means that you use that connection solely within that function, which is likely to be quite different from what you are used to. This may seem quite inefficient. You could open a connection and use it, as shown, in several different functions in a form, which seems like a waste of effort. It isn't though. Connection pooling means that you pay a bit of a cost for opening the connection the first time, but may pay nothing for every subsequent connection.

    In ADO.NET, you use ExecuteScalar to get single values, ExecuteReader to get a forward only, read-only, look through the data, and otherwise you are getting the data out of the database into a datatable, which is done using either a dataadapter or a datareader. You then do whatever work you are going to do on the datatable, which is essentially a local, in-memory, copy of whatever part of the database you need.

    That's likely quite a bit different from the design in VB6, and another reason why the converter simply can't do a good job.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    In my vb6 app, I open a connection to a database, get the data and do the needful things either with a direct connection.Execute sql command or ADO, and immediately close it.
    No reason to connect and leave it connected, I do not use any inbuilt vb6 tool to connect to a database

    I open and shut connections it all the time.

    example like this

    [CODE]
    Private Sub cmdDeleteUser_Click()
    '*********************************************
    'for demo only
    'If frmLogon.demotest = True Then NagMe
    '**********************************************
    Combo1.SetFocus

    If Combo1.Text = "" Then Exit Sub

    Removed = Combo1.Text

    'escape the '
    Removed = EscapeMe(Removed)
    'okay, it has the password in the string before the open, after the open, the password is removed.
    frmLogon.cnConnector.ConnectionString = frmLogon.Connectstring
    frmLogon.cnConnector.Open
    frmLogon.cnConnector.Execute "DELETE FROM usertable WHERE (myuser = " & nl & "'" + Removed + "')"

    'now delete the user id out of the user table in the mysql database
    If frmLogon.dbasetype = "MySQL" Then
    On Error Resume Next
    'get rid of 'combo1.text' user id
    Dim Con1 As ADODB.Connection
    Set Con1 = New ADODB.Connection
    tempconnection = frmLogon.Connectstring
    'find where the database name is and sub with 'mysql'
    a = InStr(1, tempconnection, "DATABASE=", vbTextCompare)
    If a <> 0 Then
    b = InStr(a + 1, tempconnection, ";")
    If b = 0 Then b = Len(tempconnection)
    tempconnection = Left(tempconnection, a + 8) & "mysql" & Mid(tempconnection, b)
    Con1.Open (tempconnection)
    Con1.Execute "Delete from user where user = " & nl & "'" & Removed & "'"
    Con1.Close
    Set Con1 = Nothing
    frmLogon.cnConnector.Execute "flush privileges"
    End If
    End If
    frmLogon.cnConnector.Close

    'refresh the list
    LoadCombo

    Combo1.Text = ""
    txtPassword.Text = ""
    txtChangeDate.Text = ""
    txtMyName.Text = ""

    End Sub

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    For example, I save the working connections for say 3 different databases encrypted in windows registry
    and when program starts it retrieves the connection string as in when a user wants to open a different database server, so they don't have to reenter all the connection parameters to open that connection.

    I save the last working connection for use next time program starts as mostly of course a user is going to continue with whatever current database they were using beforehand.


    'formloads even when not called
    'have to get from registry the prior stored MySQL connect string
    frmLogon.Connectstring = GetSetting(App.Title, "Login", "Connect", "")
    'then decrypt the connect string
    frmLogon.Connectstring = Encrypt(frmLogon.Connectstring, "1234567890")

    And the string is defined as a connection object,
    Public cnConnector As New ADODB.Connection

    so it knows it uses that to open a connection to a database, so when you say
    like this, (I redefine it as when you open it, all the parameters are gone and sometimes I need the info in the connect string to determine things, like the currently used database, etc...)

    frmlogon.cnconnector.connectionstring = frmlogon.Connectstring

    frmlogon.cnconnector.open, it opens the connection to a database, easy as pie.

    And it mostly continues to use that same connection throughout the entire app, just open or close as you need it.

    There is no reason to leave open database connections or you can get connection creep.

    And why do that, it likely also causes slowdowns and other multiuser issues.


    A connection string will look like this
    ?frmlogon.cnConnector
    Provider=MSDASQL.1;DRIVER={MySQL ODBC 8.0 ANSI Driver};DATABASE=front;SERVER=localhost;UID=root;PORT=3307;OPTION=34;
    Last edited by sdowney1; Apr 8th, 2024 at 11:15 AM.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    Anyone like to show me just those few simple examples I asked about?

    Just write it as it would look in vb.net, instead of vb6.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by PlausiblyDamp View Post
    Is vbrestore a valid window state in vb6? IIRC the options were vbNormal, vbMaximized, and vbMinimized - so no wonder the upgrade tool struggled with vbrestore.

    Did your vb6 application use Option Explicit?
    vbrestore is most certainly valid, it put the window back where it was before being minimized...

    I often did not bother with 'option explicit', which I know some of those conversion errors are about.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by sapator View Post
    If you use an sqlconnection the use the USING keyword . That will close and dispose the connection automatically so you don't have to worry about closing the connection.
    Example?
    I use my connection string for up to potentially 3 different servers, MSSQL, MariaDB, MySQL
    In vb6, the string is associated with an ADO connection to a database
    And it is not just left open continually, I open and close it a hundred of times in like 20 some forms.
    so it does not get set to nothing most of the time.

    That first one, SQL server conn is a test open. I will look and edit as I don't exactly recall at the moment..

    Ok, just looked and it is closing a form (frmMSDEMaster) like say a user decided to open a connection a MSSQL database, and then clicks cancel, so at that point it is never going to be used, so I just get rid of it.
    Last edited by sdowney1; Apr 8th, 2024 at 11:34 AM.

  16. #16
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,211

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by sdowney1 View Post
    vbrestore is most certainly valid, it put the window back where it was before being minimized...

    I often did not bother with 'option explicit', which I know some of those conversion errors are about.
    There is no constant called "VBRestore" in VB6. The "working" functionality is purely happenstance because of the fact that the desired behavior results from passing a value of 0, which will happen (convolutedly) when specifying a non-existent variable name.

    Try your old VB6 code and change vbrestore to diarrheaTits and it will still work. That doesn't mean diarrheaTits is a valid Window State.

  17. #17
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,547

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by sdowney1 View Post
    vbrestore is most certainly valid, it put the window back where it was before being minimized...

    I often did not bother with 'option explicit', which I know some of those conversion errors are about.
    Are you sure, the only reference I have been able to find to the Form's WindowState enum is at https://learn.microsoft.com/en-us/pr...65107(v=vs.60) and there isn't any vbRestore. I noticed in your code snippet there is no capitalisation on vbrestore unlike vbMaximized which also makes me think it isn't a valid identifier.

    If you didn't have Option Explicit On then it would be creating a new variable for you and defaulting it to 0, which maps to vbNormal, rather than giving a compiler error.

  18. #18
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,211

    Re: garbage collection error, give me an example here that is correct for this form

    There are such massive differences in database interaction between how to do it in VB6 and how to do it (best) in VB.NET that there is no way that a conversion could (or at least should) be done by a line by line translation from the VB6 code to VB.NET code. Basically, it should be completely redesigned by using the goals and concepts of the underlying VB6 code as the starting point, not the individual lines of VB6 code themselves.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    ok, well then good bye.

    Almost every thread I have on this site, I have resolved on my own!

    And others who posted here bout vb6 and database connections are also showing their own ignorance.

    Good Bye. I don't think I need to ask this vb.net forum anything in the future.
    Personally I think all these VB things today have lost most of their relevance, it should all be developed as web style apps.
    I can see MS just changes things to mke more money, not to make things easier, and vb6 is very easy, after all Basic is in the word.

    Then they opensource VS give it away, ever wonder why?
    Last edited by sdowney1; Apr 8th, 2024 at 01:25 PM.

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    528

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by PlausiblyDamp View Post
    Are you sure, the only reference I have been able to find to the Form's WindowState enum is at https://learn.microsoft.com/en-us/pr...65107(v=vs.60) and there isn't any vbRestore. I noticed in your code snippet there is no capitalisation on vbrestore unlike vbMaximized which also makes me think it isn't a valid identifier.

    If you didn't have Option Explicit On then it would be creating a new variable for you and defaulting it to 0, which maps to vbNormal, rather than giving a compiler error.
    I likely picked it up from a senior programmer friend years ago as it just worked who used to work at Control Data Corp. Thanks for being decent about that.

  21. #21
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,157

    Re: garbage collection error, give me an example here that is correct for this form

    Quote Originally Posted by sdowney1 View Post
    Then they opensource VS give it away, ever wonder why?
    No, not really. MS just realized that they were mistaken in charging most people for developer tools. They make their money off people living in the Windows ecosystem, so the more people there are writing programs for Windows, the more people will be in the Windows ecosystem. The trivial amount of money they can get by selling VS is insignificant when compared to the benefit they get from people finding it easy to write for Windows. They still do charge organizations and those with lots of cash, but for everybody else, free tools just makes too much sense.

    As to the DB, if you are going to stick with VB6, then this doesn't matter, but if you are not, you might look into the interfaces for database work available in .NET. For every object (connections, commands, datareaders, dataadapters, etc), there is generally an interface. For example, IDBConnection. This would mean that you could use specialized connections optimized for the different databases you are working with, but all interactions with them would be the same.

    Also, you said you were holding onto the connection through the life of the program, while opening and closing it multiple times. The change would be that you don't hold onto the connection at all. You create it, use it, discard it. Nothing gets held onto.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width