During Loading of form I call connectpaybox to show the list in the combobox, but after that it will result in the error
"Operation is not valid due the current state of the object"

but the combobox items that was taken from the database shows. I tried

"select CAGECOST from CAGETYPE where CAGENAME like 'Normal'"
where Normal is one of the items in cagename and it works.

Private Sub Paybox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call connectpaybox()
    Paybox.ComboBox2.SelectedIndex = 0

    End Sub
Public Sub connectpaybox()
        Dim cagename As String

        Try
            conn.ConnectionString = lubid
            conn.Open()

            '''''''''''' SQL''''''''''''''''''''
            Dim sql As String = "select cagename from CAGETYPE "
            Dim cmd As New OracleCommand(sql, conn)
            cmd.CommandType = CommandType.Text
            ''''''''' READ '''''''''
            Dim dr As OracleDataReader = cmd.ExecuteReader()
            While (dr.Read())

                Paybox.ComboBox2.Items.Add(dr.GetString(0))
            End While
        


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try

        If Paybox.ComboBox2.Items Is Nothing Then
            Paybox.ComboBox2.Text = "No available Unit"
     
        End If
    End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged


        Try

            conn.ConnectionString = lubid
            conn.Open()
            '''''''''''' SQL''''''''''''''''''''
            Dim sql As String = "select CAGECOST from CAGETYPE where CAGENAME like '" + ComboBox1.Text + "'"
            Dim cmd As New OracleCommand(sql, conn)
            cmd.CommandType = CommandType.Text
            ''''''''' READ '''''''''
            Dim dr As OracleDataReader = cmd.ExecuteReader()
            dr.Read()
            cagecost = CInt(dr.Item("CAGECOST"))

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        Finally
            conn.Close()
        End Try
    End Sub

Recommended Answers

All 5 Replies

I think you should use to load the form before accessing any objects of that form..

So

Private Sub Paybox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Paybox.Show()
        Call connectpaybox()
    Paybox.ComboBox2.SelectedIndex = 0

    End Sub

First Show the Paybox Form

Try this if u get error let me know...

By the way where u define connectplaybox() ??

Thank you for your reply. It did not work though . Every time I change the combobox , it will prompt the same error.

Nevermind. Sorry so stupid. I was referring to combobox1 instead of combobox 2. So stupid.


Sorry but for this post to be useful a bit.

"Operation is not valid due the current state of the object" tells that it cannot find the current variable .

How can I make this thread solved?

Please debug it and post the line where you get the error...

'''''''''''' SQL''''''''''''''''''''
Dim sql As String = "select CAGECOST from CAGETYPE where CAGENAME like '" + ComboBox1.Text + "'"

to

'''''''''''' SQL''''''''''''''''''''
Dim sql As String = "select CAGECOST from CAGETYPE where CAGENAME like '" + ComboBox2.Text + "'"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.