System.Data.DataRowView

Hello people. Ive got a problem with a listbox of mine. I have three lisboxes, one listbox1 is for product category, another lisbox2 is for the product itself in which it will appear after the user has chosen a product category AND another listbox3 which acts like a shopping cart in which the product that user has chosen will go to. When I click on product category there is no problem, the listbox2 will list all products, BUT when I select a product to go to my shopping cart(listbox3) this message will appear in listbox3 - System.Data.DataRowView

Here are my codes:

'I think the code below is where the problem can be found

Private Sub listCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listCategory.SelectedIndexChanged
        Dim getCategoryDesc As MySqlCommand = New MySqlCommand("Select * from category where catDesc=@field2;", connection)
        Dim reader1 As MySqlDataReader

        Try
            With getCategoryDesc
                .Parameters.AddWithValue("@field2", listCategory.Text)
            End With

            reader1 = getCategoryDesc.ExecuteReader()

            While (reader1.Read())
                txtcatNo.Text = (reader1("catNo"))
            End While
            getCategoryDesc.Dispose()
            reader1.Close()

        Catch ex As Exception

        End Try

        Try
            Dim category As String
            category = listCategory.Text
            Dim sqlcommand As String = "select product.prodNo, product.productDesc from product, category where category.catDesc='" & category & "' and product.catNo=category.catNo;"
            Dim adapter As New MySqlDataAdapter(sqlcommand, connection)
            Dim dataset As New DataSet
            adapter.Fill(dataset, "product")

            With listproduct
                .ValueMember = "prodNo"
                .DisplayMember = ("productDesc").ToString
                .DataSource = dataset.Tables("product")
                .SelectedValue = "prodNo"
                .SelectedIndex = 0
            End With

        Catch ex As Exception
        End Try

    End Sub

' What the code below does is that when I click on an item that item will be transferred to my shopping cart(listbox3)

    Private Sub listproduct_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listproduct.SelectedIndexChanged
        getprod()

        Try
            Dim i As Integer
            For i = 0 To listproduct.SelectedItems.Count - 1
                'Dim str As String =
                listItemsBought.Items.Add(listproduct.SelectedItems.Item(i))
            Next i
        Catch ex As Exception
        End Try

    End Sub

so thats my problem you guys. Help would be really appreciated

Unhnd_Exception commented: This thing,male or female, posts questions but doesn't respond to supplied answers. Don't give this thing the satisfaction of responding. -2

Recommended Answers

All 3 Replies

For one thing, you can't see if you do have an error.

All of your catch's are empty.

Try placing:

MsgBox(ex.Message)

In all of them, it will give you a little bit of an explanation of what happens.

Are you allowing the user to select a range of products?

If not, then just add the item like

listItemsBough.Items.Add(listproduct.SelctedItem())
Member Avatar for Unhnd_Exception

Your adding a DataRow to ListBoxItemsBought. Have you set the DisplayMember on ListBoxItemsBought to "productDesc" like you did to ListBoxProducts? You may want to.

When your finished your probably going to want to get the product no's from ListBoxItemsBought.

You can iterate through ListBoxItemsBought and cast the items to a DataRowView and get the productNo.
Something like this:

Dim ProductNumber As String
For Each Item In ListBoxItemsBought.Items
    If TypeOf Item Is DataRowView Then
        ProductNumber = CType(Item, DataRowView)("prodNo").ToString
    End If
Next

A couple of suggestions, if I may, while we wait for the info requested by Beginnerdev...

Because you are only retrieving one column (catNo) in your first query, you should specify that column rather than * in your query. The purpose of the query is clearer and (if you are a purist) requires only one actual query to the database. Requesting * requires two queries; one to get the field names and one to get the data.

Add some comments to your code. Even something simple like

'get category number for the selected category

and

'populate the product list with all products for the selected category

before the appropriate code blocks would be an improvement.

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.