6 Replies - 447 Views - Last Post: 10 October 2012 - 02:07 AM Rate Topic: -----

#1 Codaholic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-September 12

InvalidCast Exception was unhandled

Posted 08 October 2012 - 08:05 AM

Hello guys, i have a slight problem Am getting the error: InvalidCast Exception was unhandled when trying to execute the code below

    Private Sub BindHeaderControls()
        sqlString = "SELECT COUNT (*) AS numRows FROM tblEnrollment WHERE patientID='" & patientID & "'"
        Call ExecuteProcedures1()

        If queryResult > 0 Then
            Call OpenConnectionToDb()
            Dim objDataAdapter As New OleDbDataAdapter("SELECT * FROM tblEnrollment WHERE patientID='" & patientID & "'", objConnection)
            Dim objDataSet As DataSet
            Dim objDataView As DataView

            objDataSet = New DataSet
            objDataAdapter.Fill(objDataSet, "tblEnrollment")
            objDataView = New DataView(objDataSet.Tables("tblEnrollment"))

            mdiChildTBScreeningFrm.datePickerTodaysDate.DataBindings.Add("Text", objDataView, "todaysDate")
            mdiChildTBScreeningFrm.datePickerTodaysDate.Enabled = False
            mdiChildTBScreeningFrm.txtPID1.Text = Mid(patientID, 1, 4)
            mdiChildTBScreeningFrm.txtPID1.Enabled = False
            mdiChildTBScreeningFrm.txtPID2.Text = Mid(patientID, 6, 5)
            mdiChildTBScreeningFrm.txtPID2.Enabled = False
            mdiChildTBScreeningFrm.txtPID3.Text = Mid(patientID, 12, 1)
            mdiChildTBScreeningFrm.txtPID3.Enabled = False
            mdiChildTBScreeningFrm.datePickerDOB.DataBindings.Add("Text", objDataView, "dateOfBirth")
            mdiChildTBScreeningFrm.datePickerDOB.Enabled = False
            'mdiChildTBScreeningFrm.cboStudyPhase.DataBindings.Add("Text", objDataView, IIf("studyPhase" = 1, "SOC", "Intervention"))
            mdiChildTBScreeningFrm.cboStudyPhase.Enabled = False
            mdiChildTBScreeningFrm.cboPersonFillingOutFrm.DataBindings.Add("Text", objDataView, "persFillingOutFrm")
            mdiChildTBScreeningFrm.cboPersonFillingOutFrm.Enabled = False
            'mdiChildTBScreeningFrm.cboStudyVisit.DataBindings.Add("Text", objDataView, IIf("studyVisit" = 1, "Enrollment", "Re-Screening"))
            mdiChildTBScreeningFrm.cboStudyVisit.Enabled = False
            Call CloseConnectionToDb()
        Else
        End If
    End Sub


Please do not be concerned with connection to the db e.t.c just try to figure out what am doing wrong in the 2 code lines with comments.

Is This A Good Question/Topic? 0
  • +

Replies To: InvalidCast Exception was unhandled

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: InvalidCast Exception was unhandled

Posted 08 October 2012 - 08:24 AM

It would appear that you're trying to cast a string to a numeric type

IIf("studyVisit" = 1 ???

Perhaps that should be
IIF(studyVisit = 1


But that's only a guess, because I have no idea what studyVisit even represents here.

This post has been edited by CharlieMay: 08 October 2012 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 Codaholic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-September 12

Re: InvalidCast Exception was unhandled

Posted 08 October 2012 - 10:58 PM

Hey Charlie, sorry for the late response. studyVisit is a column in the database of datatype number.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: InvalidCast Exception was unhandled

Posted 09 October 2012 - 04:09 AM

OK, so You're going to have to specify that as IIF doesn't know that "studyVisit" is anything more than a string because, in this context it actually is just a string.

If you wanted to see what your studyVisit contained how would you go about writing it? For example, if you wanted to display that value in a MessageBox.

What I'm trying to push you toward is that you will need to use this same method in order to compare the columns contents in your IIF statement.
Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: InvalidCast Exception was unhandled

Posted 09 October 2012 - 05:46 AM

Try a simple test, and use If(... instead of IIF(... in those 2 commented lines of code.
Was This Post Helpful? 0
  • +
  • -

#6 Codaholic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-September 12

Re: InvalidCast Exception was unhandled

Posted 10 October 2012 - 01:54 AM

View PostCharlieMay, on 09 October 2012 - 04:09 AM, said:

OK, so You're going to have to specify that as IIF doesn't know that "studyVisit" is anything more than a string because, in this context it actually is just a string.

If you wanted to see what your studyVisit contained how would you go about writing it? For example, if you wanted to display that value in a MessageBox.

What I'm trying to push you toward is that you will need to use this same method in order to compare the columns contents in your IIF statement.

Was This Post Helpful? 0
  • +
  • -

#7 Codaholic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-September 12

Re: InvalidCast Exception was unhandled

Posted 10 October 2012 - 02:07 AM

Hey Charli, how can compare the column contents of the study visit column using IIF method?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1