Error while closing SQL Connection

Posted by Wickedman84 on Stack Overflow See other posts from Stack Overflow or by Wickedman84
Published on 2014-04-30T09:35:06Z Indexed on 2014/05/28 9:27 UTC
Read the original article Hit count: 138

Filed under:
|
|

I have a problem with closing the SQLconnection in my application. My application is in VB.net. I have a reference in my application to a class with code to open and close the database connection and to execute all sql scripts.

The error occurs when i close my application. In the formClosing event of my main form I call a function that closes all the connections. But just before I close the connections I perform an SQLquery to delete a row from a table with the function below.

Public Function DeleteFunction(ByVal mySQLQuery As String, ByVal cmd As SqlCommand) As Boolean
    Try
        cmd.Connection = myConnection
        cmd.CommandText = mySQLQuery
        cmd.ExecuteNonQuery()
        Return True

    Catch ex As Exception
        WriteErrorMessage("DeleteFunction", ex, Logpath, "SQL Error: " & mySQLQuery)
        Return False
    End Try
End Function

In my application I check the result of the boolean. If it returns True, then i call the function to close the database connection.

The returned boolean is True and the requested row is deleted in my database. This means i can close my connection which I do with the function below.

Public Sub DatabaseConnClose()
    myCommand.CommandText = ""
    myConnection.Close()

    myCommand = Nothing
    myConnection = Nothing
End Sub

After executing this code I receive an error in my logfile from the DeleteFunction. It says: "Connection property has not been initialized."

It seems very strange to receive an error from a function that was completely executed, or am i wrong to think that?

Can anyone tell me why I receive this error and how I can solve the problem?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server