Solved

System.Data.DataRowView ComboBox error with VB.NET

Posted on 2008-10-23
8
3,728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
Part One of the two-part Q&A series with MalwareTech.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

623 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