Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

System.Data.DataRowView ComboBox error with VB.NET

Posted on 2008-10-23
8
Medium Priority
?
3,939 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:logoncom
  • 4
  • 4
8 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22792205
Hi logoncom,


Try to change to this:



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

Open in new window

0
 

Author Comment

by:logoncom
ID: 22792288
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.
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 22792311
You should define first the DisplayMember and ValueMember before the DataSource . If you do this when the source is assigned you already have that information.
Can you show more code ? (Selected Index Changed)
Do you see the items ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:logoncom
ID: 22792571
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

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22797057
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.
0
 

Author Comment

by:logoncom
ID: 22797133
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
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22797152
>>  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.
0
 

Author Closing Comment

by:logoncom
ID: 31509502
This is what I ended up doing along with setting datasource to nothing first
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question