Solved

System.Data.DataRowView ComboBox error with VB.NET

Posted on 2008-10-23
8
3,552 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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