Invalid Operation Exception was unhandled

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lotus18
    Contributor
    • Nov 2007
    • 865

    Invalid Operation Exception was unhandled

    WaAh!

    My form was working fine earlier but later it displays Invalid Operation Exception was unhandled. I don't know what's wrong with my form every time I load it.I checked its codes but I find what's wrong with it.

    If I'm going to remove Transaction.Cou rses.Show(False , txtSearch.Text) at line #2 of frmCourses it works fine. Of course I cannot filter the records : (

    This is my sample codes

    clsCourses
    [CODE=vbnet]
    #Region " Variable Declarations... "
    Private ModColor As Boolean
    Private SQLString As String
    Private i As Integer
    Private adItem As ListViewItem
    Private rsCourses As OleDbDataReader
    Private cmdCourses As New OleDbCommand
    #End Region

    Public Sub Show(ByVal ShowAllList As Boolean, Optional ByVal Search As String = vbNullString)
    Windows.Forms.C ursor.Current = Cursors.WaitCur sor
    If ShowAllList Then
    SQLString = "Select * From Courses Order By Title ASC"
    Else
    SQLString = "Select * From Courses Where CourseID Like '%" & Search & _
    "%' Or Title Like '%" & Search & "%' Or Description Like '%" & _
    Search & "%' Order By Title ASC"
    End If

    Call SetConnection()
    cmdCourses = New OleDbCommand(SQ LString, dbConnection)
    cmdCourses.Comm andType = CommandType.Tex t
    rsCourses = cmdCourses.Exec uteReader

    frmCourses.lvwC ourses.Items.Cl ear()
    While rsCourses.Read
    adItem = frmCourses.lvwC ourses.Items.Ad d(rsCourses(0), 0) 'Course ID
    adItem.SubItems .Add(rsCourses( 1)) 'Title
    adItem.SubItems .Add(rsCourses( 2)) 'Description
    End While

    frmCourses.lblT otalRecords.Tex t = "Total Records: " & frmCourses.lvwC ourses.Items.Co unt

    If frmCourses.lvwC ourses.Items.Co unt > 0 Then
    frmCourses.lblS howStatus.Visib le = False
    Else
    frmCourses.lblS howStatus.Visib le = True
    End If

    For i = 0 To frmCourses.lvwC ourses.Items.Co unt - 1
    If ModColor = True Then
    frmCourses.lvwC ourses.Items(i) .BackColor = Color.AliceBlue
    ModColor = False
    Else
    frmCourses.lvwC ourses.Items(i) .BackColor = Color.White
    ModColor = True
    End If
    Next

    rsCourses.Close ()
    Call CloseConnection ()
    Windows.Forms.C ursor.Current = Cursors.Default
    End Sub[/CODE]
    frmCourses
    [CODE=vbnet]Private Sub txtSearch_TextC hanged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles txtSearch.TextC hanged
    Transaction.Cou rses.Show(False , txtSearch.Text)
    End Sub

    Private Sub frmCourses_Load (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    Main.SetChildFo rm(frmMain, Me)
    Transaction.Cou rses.Show(True)
    End Sub
    [/CODE]

    Rey Sean
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    An integer, eg an id, can't be like another incl in an sql statement. Suggest using try catch blocks and/ or other error handling. HTH.

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      Originally posted by kenobewan
      An integer, eg an id, can't be like another incl in an sql statement. Suggest using try catch blocks and/ or other error handling. HTH.
      Hi

      Thanks for your reply : )

      I've used try and catch blocks as what you've said but still it gives me an error result, An error occurred creating the form. See Exception.Inner Exception for details. The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'

      BTW, what do you mean by incl?

      Rey Sean

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well what I saw turned out to be exactly what the error was giving you. So I think I am right on this.

        In your form_load function (which gets called everytime it's shown) you are trying to SHOW it again. Which would lead to infinite recurrsion.
        Remove the unneeded .Show() call like you said to make it work.
        It shouldn't be there anyway.

        Comment

        • lotus18
          Contributor
          • Nov 2007
          • 865

          #5
          Originally posted by Plater
          Well what I saw turned out to be exactly what the error was giving you. So I think I am right on this.

          In your form_load function (which gets called everytime it's shown) you are trying to SHOW it again. Which would lead to infinite recurrsion.
          Remove the unneeded .Show() call like you said to make it work.
          It shouldn't be there anyway.
          If that is the reason I think I should create another method for clsCourses for filtering the records. But I don't think that is the best idea because I am going to create another method again with almost the same as with the showing of records the only difference between them is their sql statement. Can you give me another idea how to handle this?

          Rey Sean

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Well lets start off with why are you continuing to make another instance of your class? (Or at least calling Show many times)
            If you want to hide the form and then unhide it, use the Visible property?

            Comment

            Working...