1

I have build a WindowForm application that has a combobox which has a Data Binding Mode to a View on SQL Database and textbox.

The View query: "SELECT [CompanyName],CountryName FROM [CompanyRefTable]"

The members of the combobox are as follow: DataSource: CDALLENTITIESLISTBindingSource DisplayMember:CompanyName ValueMember: CompanyName SelectedValue: CompanyName

I have done some modifications to the code (updated on page)

Now Im getting the values from the second column:)

My code:

    Dim rowView As DataRowView = TryCast(Me.cmbEntity.SelectedItem, DataRowView)
    If (Not rowView Is Nothing) Then
        Dim row As DataRow = rowView.Row
        Dim subjectName As String = DirectCast(row.Item("CountryName"), String)
        Me.txtCountry.Text = subjectName.ToString
    End If

Thank you for your help!

Mirosław Gądek
  • 118
  • 2
  • 10
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it), I am sure this thread will explain and answer in further details about your error you are receiving. – Trevor Jan 23 '19 at 14:16
  • 1
    There's no such thing as a `ComboBox` that's linked to a SQL DB. Presumably the actual scenario is that you are populating a `DataTable` from SQL Server and then binding that to the `ComboBox` via the `BindingSource`. A `DataTable` is a `DataTable`. Where the data came from isn't relevant tot he control. – jmcilhinney Jan 23 '19 at 14:18
  • The `SelectedValue` is a value from the column specified by the `ValueMember`. Does it really make sense `CountryName` value, index that and then call `ToString` on the result? That's rhetorical. It doesn't make sense. I'm guess that what you're actually trying to do is get the whole row and then get a value from the column at index 0. In that case, it's the `SelectedItem` you want rather than the `SelectedValue`, although using the `Current` Property of the `BindingSource` would be more appropriate. You then cast as type `DataRowView` and then index that by column name or ordinal. – jmcilhinney Jan 23 '19 at 14:21
  • You'ld have to first check whether the `SelectedIndex` is `-1`, it happens (always). Then, `Me.cmbEntity.SelectedValue(1).ToString`? The SelectedValue is the current Value, corresponding to the `ValueMember`, of the current `SelectedItem`. See [ListControl.GetItemText()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listcontrol.getitemtext). – Jimi Jan 23 '19 at 14:23
  • Thank you jmcilhinney for your help. – Mirosław Gądek Jan 23 '19 at 15:31

1 Answers1

1

Thank you to :jmcilhinney for your comment it helped me in finding solution:)

I found an solution for my issue on internet:

    Dim rowView As DataRowView = TryCast(Me.cmbEntity.SelectedItem, DataRowView)
    If (Not rowView Is Nothing) Then
        Dim row As DataRow = rowView.Row
        Dim subjectName As String = DirectCast(row.Item("CountryName"), String)
        Me.txtCountry.Text = subjectName.ToString
    End If
Mirosław Gądek
  • 118
  • 2
  • 10