StrataFrame Forum

How to test that a connection is valid...

http://forum.strataframe.net/Topic12401.aspx

By Greg McGuffey - 11/4/2007

I have a method that checks that a DataSource is valid by attempting to connect to it. However, when using ES, the method fails. I've tried two methods, both work if I'm setting a sql data source and both fail if I use ES.



Method 1

This method gets a DbConnection from the DataSource using the CreateBlankDbConnection method, then attempt to open it:

Private Function ValidateConnection(ByVal key As String) As Boolean

'-- Establish return var

Dim connOK As Boolean

'-- Validate the connection

Using testConn As Data.Common.DbConnection = DataBasics.DataSources(key).CreateBlankDbConnection

Try

testConn.Open()

connOK = True

Catch ex As Exception

'-- Connection failed

connOK = False

End Try

End Using

'-- Return result

Return connOK

End Function




Method 2

This method attempts to use a BO (typically one that has little data in the underlying table) to attempt to fill it. The idea here is that if the fill method works (no exceptions) then the connection is valid:

Private Function ValidateAppDbConnection() As Boolean

'-- Establish return var

Dim connOK As Boolean

'-- Try to fill a BO. If there is no exception we have a connection.

Try

Using bo As New MyBO()

bo.FillByPrimaryKey(1)

End Using

connOK = True

Catch ex As Exception

connOK = False

End Try

'-- return results

Return True 'connOK

End Function




I'm calling these in the SetDataSources event handler. Again these both work when not using ES.



So, I'm wondering why they don't work and then I'm wondering how I'd check the connection when connecting to the data via ES.



Thanks!
By Greg McGuffey - 11/5/2007

bump... Blink
By StrataFrame Team - 11/6/2007

The first one won't work because it's trying to use the SqlConnection object directly...

However, the second one should work fine... what's the exception that you're getting on the second one when you're using an ES data source?