Link to home
Start Free TrialLog in
Avatar of logoncom
logoncom

asked on

System.Data.DataRowView ComboBox error with VB.NET

I have a combobox whose datasource is a datatable that I fill using a Stored Procedure.

To prevent the combobox selected index changed event from fireing when it is not suppose to, I set the combobox = string.empty after it is filled. Then I use:

if combobox.text = string.empty then
(Dont Do Anything)
Else (Do something)
endif

My problem is that combobox.text = System.Data.DataRowView

How can I make sure my program does nothing if my combobox = System.Data.DataRowView
Dim dt As New Data.DataTable
                Using conn As New Odbc.OdbcConnection(OdbcConnection)
 
                    Using cmd As New Odbc.OdbcCommand("SelectCustno", conn)
                        cmd.CommandType = CommandType.StoredProcedure
 
                        Using da As New Odbc.OdbcDataAdapter(cmd)
 
                            da.Fill(dt)
 
                            With cbbCompanyName
 
                                .DataSource = dt
                                .DisplayMember = "ccustno"
                                .ValueMember = "ccustno"
 
                            End With
 
                        End Using
                    End Using
                End Using
 
            Catch ex As System.Exception
 
                MessageBox.Show(ex.Message)
 
            End Try
 
        End If

Open in new window

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Hi logoncom,


Try to change to this:



        .DisplayMember = "ccustno"
        .ValueMember = "ccustno"
        .DataSource = dt

Open in new window

Avatar of logoncom
logoncom

ASKER

That does not work.  Now it is making everything in the combobox equal to System.Data.DataRowView which does me no good.

The problem seems to be that when .displaymember = "ccustno" line is hit, my combobox selected_indexchanged event fires.  Since I dont have a value yet  my program crashes...Error says "system.data.datarowview can not be converted to string".

How can I make it so that the combobox event does not fire so early?  That would solve my problem.
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It happens on the SelectedValueChanged.  

VcCustno is a String variable and is set to be nothing at this point since I dont have the data.  However, When I try to fill the combobox, this event fires and the value is neither Nothing or String.Empty... the value at this point is datarowview.  So when VcCustno = cbbcompanyname.selectedvalue (system.data.datarowview)... I get the conversion error and program crash.
 Private Sub cbbCompanyName_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbCompanyName.SelectedValueChanged
 
        If isgoldmine = False Then
 
            If Not vcCustno Is Nothing And vcCustno <> String.Empty Then
                If cbbCompanyName.Text <> String.Empty Then
 
                    vcCustno = cbbCompanyName.SelectedValue
 
                End If
            End If
        End If
 
    End Sub

Open in new window

Just one question: if you use the same field for the display and the value you don't need to use bought. You can show only the displaymenber (that I think it's fine) and use cbbCompanyName.Text to your work.
I got it to work by first setting the combobox.datasource = nothing and then refilling it.  Issue was I was using the Same combobox to hold two different DataTables depending on User selection... and each one had different .displaymember and .valuemember.  That is what I think was happening because now it works
>>  Issue was I was using the Same combobox to hold two different DataTables depending on User selection... and each one had different .displaymember and .valuemember.
Ok, you didn't mentioned that! I was finding strange not to work that way.
This is what I ended up doing along with setting datasource to nothing first