Both of these properties have a default value of 30 seconds but I am setting them to something much shorter (8) and my application does not seem to recognize the timeout. Even if I allow the default value of 30 seconds to elapse I still am waiting on the timeout.
I am trying to use the connecttimeout to notify my client application that the remote sql server is not available before I try and execute a sql transaction.
I am not sure why this is not working though
, the sqlexception for timing out has worked for me before and all I did was set a non-default property. The only change here is that this is a MDI type of project, which shouldn't matter.
Here's my code:
Code:
Public Function sqlSelect(ByVal query As String, ByVal outputtype As SelectType, Optional ByVal resultsset_name As String = "results", Optional ByVal commandtype As CommandType = CommandType.Text, Optional ByVal parameters As List(Of SqlParameter) = Nothing) As Object
Dim cmd As SqlCommand = Nothing
Try
cmd = GetSQLCommand(query, commandtype, parameters)
'command will already have parameters attached and timeout set to 8 here
Catch sql As SqlException
MessageBox.Show("A sql error was caught during execution")
Catch ex As Exception
exHandler(ex)
End Try
Debug.Assert(cmd.CommandTimeout = 8)
Dim outputdata As Object = Nothing
Select Case outputtype
Case SelectType.Sqldatareader
outputdata = CType(outputdata, SqlDataReader)
outputdata = FillDataReader(cmd)
Case SelectType.datatable
outputdata = CType(outputdata, DataTable)
outputdata = FillDataTable(cmd)
Case SelectType.dataset
outputdata = CType(outputdata, DataSet)
outputdata = FillDataSet(cmd)
Case SelectType.xml
'todo: future expansion
End Select
Return outputdata
End Function
Private Function FillDataTable(ByVal objcmd As SqlCommand, Optional ByVal strtable As String = "results") As DataTable
Try
Dim da As New SqlDataAdapter(objcmd)
Dim dt As New DataTable(strtable)
da.Fill(dt) '<-- this line causes application deadlock
Return dt
Catch sql_ex As SqlException
Throw sql_ex
Catch ex As Exception
Throw ex
End Try
End Function
Private Function GetSQLCommand(ByVal qrytext As String, Optional ByVal commandtype As CommandType = CommandType.Text, Optional ByVal paramscollection As List(Of SqlParameter) = Nothing) As SqlCommand
Try
Dim objcmd As SqlCommand = New SqlCommand(qrytext, Me.Connection())
objcmd.CommandType = commandtype
objcmd.CommandTimeout = 8
If paramscollection Is Nothing Then
Return objcmd
Else
If paramscollection.Count > 0 Then
For Each param As SqlParameter In paramscollection
objcmd.Parameters.Add(param)
Next
End If
Return objcmd
End If
Catch sqlex As SqlClient.SqlException
'exHandler(sqlex)
Throw sqlex
Catch ex As Exception
exHandler(ex)
Throw ex
End Try
End Function