Thanks! I wasn't sure if the Try code would continue or not if an exception
was "caught". Now I know it doesn't.

"Marina" <mzlatkina@hotmail.com> wrote in message
news:u7IKm0QYDHA.2548@TK2MSFTNGP09.phx.gbl...
> There is no need to check if the connection is open. It can't be, you just
> created it.
>
> I would write it in the following way:
>
> Public Function ConnectToDatabase(ByVal strConnectionString As String)
> As Boolean
> Try
> cnnMyConnection = New SqlConnection(strConnectionString)
> cnnMyConnection.Open()
>
> Return True
> Catch ex As Exception
> HandleError(ex, "SqlDataHelper.ConnectToDatabase")
> Return False
> End Try
>
> End Function
>
>
> "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
> news:e0i28vQYDHA.384@TK2MSFTNGP12.phx.gbl...
> > I created a custom utility function called ConnectToDatabase, which does
> > just that. It should return True if it went ok, False if not. Is this
> the
> > most efficient code or is there an easier way (esp in regards to the
> Return
> > value)?
> >
> > Private cnnMyConnection As SqlConnection
> >
> > Public Function ConnectToDatabase(ByVal strConnectionString As
String)
> > As Boolean
> > Dim bSuccess As Boolean = True
> >
> > Try
> > cnnMyConnection = New SqlConnection(strConnectionString)
> > If cnnMyConnection.State <> ConnectionState.Open Then
> > cnnMyConnection.Open()
> > End If
> > Catch ex As Exception
> > HandleError(ex, "SqlDataHelper.ConnectToDatabase")
> > bSuccess = False
> > End Try
> >
> > If bSuccess Then
> > Return True
> > Else
> > Return False
> > End If
> > End Function
> >
> >
>
>