Domanda

The following function is used by several different methods:

Public Shared Function SqlScalar(ByVal sql As String) As Object
    Dim cmd As New OleDbCommand(sql, frmMain.con)

    frmMain.con.Open()
    Dim Scalar As Object = cmd.ExecuteScalar
    frmMain.con.Close()

    Return Scalar
End Function

con is defined in frmMain as follows:

Public Shared con As OleDbConnection
...
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= G:\mypath\mydatabase.accdb")

The crash patterns I've unraveled so far are pretty strange, so I'll do my best to describe them.

When it crashes (while debugging) it does so on the frmMain.con.Close() line, after successfully assinging a value to Scalar. VS doesn't give the standard Exception Thrown information, but only gives the following:

Crash message

Clicking "Debug the progam" just closes the program.

Now, SqlScalar is called upon by methods A, B, C, D, and E. It runs just fine for methods A - D, but crashes when called by E.

Things I've tried:

  • If I comment out any of methods A - D, then it works just fine with E. I thought that maybe it had something to do with just the number of times it had been called, so I put it in a loop that just called SqlScalar repeatedly. It didn't crash after 100+ iterations.

  • If I run E earlier (before C, for example), everything runs fine. However, this problem has popped up in different areas for some time and I'd really like to get it ironed out.

  • Toggling the project settings "Enable unmanaged code debugging" and "Enable the Visual Studio hosting process" didn't help and/or caused other problems.

Edit: Another odd symptom is that sometimes, if I insert a breakpoint somewhere in the script and let it sit for a few minutes after breaking, it crashes with the same error window shown above.

Edit II: I’ve tested the script on several machines (both via VS and the exe), and on some machines it crashes, on some it doesn’t. If it crashes in VS, then the exe does as well, both giving the same error message seen above. All machines are running Win7 64 bit.

È stato utile?

Soluzione

This is Dana Wyatt from Hands On Technology Transfer. Here are some thoughts I have:

1) I concur that your connection object needs to be LOCAL to the method. When you reference a public variable in another form, it may be null if the form doesn't exist.

Public Shared Function SqlScalar(ByVal sql As String) As Object

    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= G:\mypath\mydatabase.accdb")

    Dim cmd As New OleDbCommand(sql, frmMain.con)

    con.Open()
    Dim Scalar As Object = cmd.ExecuteScalar
    con.Close()

    Return Scalar
End Function

2) Somewhere along VS 2010, it quit throwing exceptions when a connection string is partly wrong. So you can have an open that doesn't work and not fail until later in the method. SO MAKE ABSOLUTELY SURE your connection string is 100% right. It would be best to put it in the section of a config file and ALWAYS load it from there.

3) Finally, the buggy behavior on one machine but not another leads me to believe you have a driver issue on one or more machines for the accdb databases. Make sure you have the latest Office System Drivers, engines, etc downloaded.

http://www.microsoft.com/en-us/download/details.aspx?id=13255

Try all of this. Let us know at HOTT if this works. I believe you are e-mailing Chris E. And if it doesn't work, we will try to think of more issues.

Personally, I believe it isn't ONE problem, but a combinaion of several... kinda like three bad wheels on a grocery buggy!!! : )

-Dana

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top