Solved

System.Data.DataRowView ComboBox error with VB.NET

Posted on 2008-10-23
8
3,442 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

912 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

24 Experts available now in Live!

Get 1:1 Help Now