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
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.selectedval ue (system.data.datarowview). .. I get the conversion error and program crash.
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.selectedval
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
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.
ASKER
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.
Ok, you didn't mentioned that! I was finding strange not to work that way.
ASKER
This is what I ended up doing along with setting datasource to nothing first
Try to change to this:
Open in new window