can anyone help me about this error. "Invalid attempt to call metadata when reader is closed". Thnx in advance.

Public Sub DisplayCust(ByVal lv As ListView)
        If cnSQL.State = ConnectionState.Open Then cnSQL.Close()
        cnSQL.Open()
        cmd = cnSQL.CreateCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "sp_DisplayCust"

        Dim param = New SqlClient.SqlParameter
        param.ParameterName = "@cust_no"
        param.SqlDbType = SqlDbType.VarChar
        param.Value = lv.SelectedItems(0).Text
        cmd.Parameters.Add(param)

        objRead = cmd.ExecuteReader

        Try
              While objRead.Read()
                  With frmCustomer
                    .txtCustCode.Text = (objRead("cust_no") & "")
                    .cmbSM.Text = (objRead("sm_name") & "")
                    .txtCustname.Text = (objRead("cust_name") & "") < Error in this line
                    .txtaddress.Text = (objRead("address") & "")
                    .txtRemarks.Text = (objRead("remarks") & "")
                    .txtEmail.Text = (objRead("email_add") & "")
                    End With
                End While
                objRead.Close()
                cnSQL.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Data Error")
            Exit Sub
        End Try
    End Sub

Recommended Answers

All 2 Replies

Does 'cust_name' exist in the returning table?
Does the error occur on the first iteration or later?

See what happens if you expand the Try..Catch with a Finally and put objRead.Close() and cnSQL.Close() inside that.

remove objRead.Close() from code and try

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.