Solved

System.Data.DataRowView ComboBox error with VB.NET

Posted on 2008-10-23
8
3,407 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 500 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now