I have been tasked to rewrite an application I wrote in VBA to VB. I am literally learning as I go! In VBA I used the ADODB com for connection to a database, but this doesn't seem to work very well in VB. I have changed to the sqlClient com thats in VB 2010 and for some reason I am having issues with counting records in a database. The code I have is:
sql = "SELECT MAX(fundDays) AS totals FROM WOFT_tbl_funds WHERE polNumber = '" & PolNumberField1.Text & "' AND fundReqMeth = 'Options' " cnn = New SqlConnection(ConnString) Try cnn.Open() cmd = New SqlCommand(sql, cnn) reader = cmd.ExecuteReader() While reader.Read() optiondays = CInt(CStr(reader.Item(0))) End While reader.Close() cmd.Dispose() cnn.Close() MsgBox(optiondays) Catch ex As Exception MsgBox("Can not open connection!") End Try
This always throws the Exception error? I have tried replacing the variable with a constant in the query to no avail. I was originally having issues with my queries as ADOBD recommends you close out the query with a ;, however this throws the exception in VB
Can someone point me in the right direction? Is reader the way to do this or should I be reading about something else?