2 Replies - 1192 Views - Last Post: 21 September 2012 - 02:53 AM Rate Topic: -----

#1 herghost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-September 12

help with sqlclient and select max() as, connection not open

Posted 21 September 2012 - 02:26 AM

Hi guys

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?

Thanks

Dave

Is This A Good Question/Topic? 0
  • +

Replies To: help with sqlclient and select max() as, connection not open

#2 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 445
  • View blog
  • Posts: 3,084
  • Joined: 18-September 06

Re: help with sqlclient and select max() as, connection not open

Posted 21 September 2012 - 02:34 AM

Where does it throw the exception, and what is the exception?
Was This Post Helpful? 0
  • +
  • -

#3 herghost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-September 12

Re: help with sqlclient and select max() as, connection not open

Posted 21 September 2012 - 02:53 AM

Sorry, please ignore this post, I somehow managed to post a dupe - topic can be found http://www.dreaminco...ction-not-open/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1