Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27159

VS 2008 SQL Connection issues with sleep/power saving mode

$
0
0
Hi All,

I have the following error occur for some users occasionally, most often it seems when the computer enters sleep mode. (The front end has background threads which periodically check certain things with the back end).

'Fill: SelectCommand.Connection property has not been initialized.'

The following is my code:

Code:

Public Function GetAndOpenConnection() As SqlConnection

        Dim Conn As New SqlConnection("Data Source=" & DBstring & ";Database=**;User ID=**;Password=**")

        While Conn.State = ConnectionState.Closed
            Try
                Conn.Open()
                HaveConnection = True
            Catch ex As Exception
                HaveConnection = False
                If MsgBox("The backend data source can be not be found. Retry?" & vbCrLf & "Cancel to exit application." & _
                          vbCrLf & "Message: " & ex.Message, MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel Then
                    Application.Exit()
                    Exit Function
                End If
            End Try
        End While

        Return Conn

    End Function

    Public Function FillDataTable(ByVal strSQL As String, Optional ByVal NewDBConn As SqlConnection = Nothing) As Datatable

        Dim DBConn As SqlConnection = GetAndOpenConnection()
        Dim SqlDa As New SqlDataAdapter()
        Dim dt As New Datatable

        SqlDa.SelectCommand = New SqlCommand(strSQL, DBConn)

        SqlDa.Fill(dt)  'ERROR is thrown here
        dt.Sql = strSQL

        SqlConnection.ClearPool(DBConn)
        DBConn.Close()

        Return dt

    End Function

What is confusing me on this is that by getting to the point it does, the connection must be returned without issue, but fails when it is actually used.

Its a VS2008 project on .net 3.5, connected to an MSSQL express server on a networked machine.

TIA

Viewing all articles
Browse latest Browse all 27159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>