AdorableVB
AdorableVB

Reputation: 1393

Data Table has null values

on FbuildingSettings.vb - Form

Public camButtonDtable As DataTable

Then I used it in Fbuilding.vb - Form

Dim names = From row In FbuildingSettings.camButtonDtable.AsEnumerable() Select row.Field(Of String)("Building") Distinct
    For Each word In names
        ComboBox1.Items.Add(word)
    Next

Pretty much do what I want, get each of the distinct data from a column. So I used that code in FbuildingSettings.vb

Dim names = From row In camButtonDtable.AsEnumerable() Select row.Field(Of String)("Building") Distinct
    For Each word In names
        comboBuilding.Items.Add(word)
    Next

I get the error:
enter image description here
Tried this : Public camButtonDtable As New DataTable - it did compiled, but the comboBox does not display a thing.
What am I doing wrong?

Upvotes: 0

Views: 125

Answers (2)

AdorableVB
AdorableVB

Reputation: 1393

Finally found the solution (this is the one I used):

FbuildingSettings.camButtonDtable = databaseFunctions.GetDataTable(queryBtn) 'to iniatialize data on cambtndtable

This should be something like this (not really sure):

Dim queryBtn As String = "SELECT * FROM tblCameraButtons"
FbuildingSettings.camButtonDtable = <DataTable>.Fill(queryBtn)

Upvotes: 0

Cause

  1. Your title is irrelevant to the thrown exception.
  2. At this point, the problem is not whether or not the DataTable is populated, but rater if the DataTable is instantiated.
  3. Your DataTable is not instantiated.

Your error occurs at:

<DataTable>.AsEnumerable()

From reflector, System.Linq.Enumerable:

Public Shared Function AsEnumerable(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IEnumerable(Of TSource)
    Return source
End Function

Solution

  1. Make sure the table exists.
  2. Ensure that the table are properly initialized.

Public Class FbuildingSettings

    Public Sub New()
        Me.InitializeComponent()
        Me.camButtonDtable = New DataTable()
        Me.camButtonDtable.Columns.Add("Building", GetType(String))
        Me.camButtonDtable.AcceptChanges()
    End Sub

    Public ReadOnly camButtonDtable As DataTable

End Class

Upvotes: 1

Related Questions