Hi guys,
I am using .net cf 2.0.

I am trying to get the maximum value from a column in a table in my database and then call a function using the value returned.

Here's the code i have:
VB Code:
  1. sqlCmd = Database.sqlConn.CreateCommand
  2.             With sqlCmd
  3.                 .CommandText = "SELECT MAX(seqOrder) FROM questionnaire_results WHERE visitID = @visitID AND qID = @qID"
  4.                 .Parameters.Add(New SqlCeParameter("@visitID", GetCurrentVisit()))
  5.                 .Parameters.Add(New SqlCeParameter("@qID", qID))
  6.                 maxSeqOrder = .ExecuteScalar.ToString
  7.                 If IsDBNull(maxSeqOrder) OrElse IsNothing(maxSeqOrder) Then
  8.                     maxSeqOrder = 0
  9.                 End If
  10.                 Select Case maxSeqOrder
  11.                     Case "0"
  12.                         DisplayQuestion(0)
  13.                     Case Else
  14.                         DisplayQuestion((CInt(maxSeqOrder) / 5) - 1)
  15.                 End Select
  16.             End With

Now everytime i run this code i keep getting an invalidcast exception. maxSeqOrder is declared (currently) as a string and DisplayQuestion needs to be passed an integer. I have tried using maxseqorder as an integer but i get the same exception. I have put debug lines in and found that the exception is thrown at the line
VB Code:
  1. maxSeqOrder = .ExecuteScalar.ToString
when maxseqorder is a string and a integer. I have tried to get help from a colleaugue but neither of us can see why the exception is being thrown.

Can anybody else see where the problem is coming from?

Thanks in advance for any help