Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

System.NullReferenceException occurred on SelectionChange Event of ComboBox

$
0
0

Here is the issue,

I've free tables in my database : Departement, Commune, Arrondissement, (it means in english Departement, Township, District)

So when the user choose a departement from the ComboBox named q7, only its Communes(Townships) will be loaded in comboboxq8, and when he chooses a commune of this Departement, only its Arrondissement(District) will be loaded in the comboboxq9.

The program works well, when the user choose items, in the good order ie combobox q7, then q8, then q9.

but After chosing for example an Arrondissement, which is the 3rd level, and he want to Change theDepartement, the program crashes with this:

An unhandled exeption of type 'System.NullReferenceException' occurred in System.Data.dll

Additional information : Object reference not set to an instance of an object

Here is my code

in XAML

<Window.Resources><!--Style for LABELS -->        <Style x:Key="LabelOnTab"><Setter Property="Control.Background" Value="{x:Null}" /><Setter Property="Control.Foreground" Value="#FFFF8102" /><Setter Property="Control.FontWeight" Value="ExtraBold" /><Setter Property="Control.VerticalContentAlignment" Value="Center" /><Setter Property="UIElement.Opacity" Value="100" /></Style><!--Style for TEXTBOX--><Style x:Key="InputOnTab"><Setter Property="Control.Background" Value="#FFFF8102" /><Setter Property="Control.Foreground" Value="Black" /><Setter Property="Control.VerticalContentAlignment" Value="Center" /><Setter Property="UIElement.Opacity" Value="0.8" /></Style><local:SNIGDoFFEDataSet x:Key="SNIGDoFFEDataSet"/><CollectionViewSource x:Key="DepartementViewSource" Source="{Binding Departement, Source={StaticResource SNIGDoFFEDataSet}}"/><!--ViewSource for COMMUNES by DEPARTEMENT--><CollectionViewSource x:Key="CommuneParDepartementViewSource" Source="{Binding Commune, Source={StaticResource SNIGDoFFEDataSet}}"/><!--ViewSource for ARRONDISSEMENTS by COMMUNE--><CollectionViewSource x:Key="ArrondissementParCommuneViewSource" Source="{Binding Arrondissement, Source={StaticResource SNIGDoFFEDataSet}}"/></Window.Resources>


<Label x:Name="labQ7" Foreground="AliceBlue" Style="{StaticResource LabelOnTab}"  Content="Q7. Département:" Height="28" Margin="10,0,246,191"/><ComboBox x:Name="q7" Style="{StaticResource InputOnTab}" HorizontalAlignment="Left" Height="23" Margin="160,0,0,0" VerticalAlignment="Top" Width="202" IsReadOnly="True" DisplayMemberPath="Libelle" SelectionChanged="q7_SelectionChanged"><ComboBox.ItemsSource><Binding Source="{StaticResource DepartementViewSource}"/></ComboBox.ItemsSource></ComboBox><Label x:Name="labQ8" Foreground="AliceBlue" Style="{StaticResource LabelOnTab}"  Content="Q8. Commune:" Height="28" Margin="10,28,246,162"/><ComboBox x:Name="q8" Style="{StaticResource InputOnTab}" HorizontalAlignment="Left" Height="23" Margin="160,28,0,0" VerticalAlignment="Top" Width="202" IsReadOnly="True" DisplayMemberPath="Libelle" SelectionChanged="q8_SelectionChanged"><ComboBox.ItemsSource><Binding Source="{StaticResource CommuneParDepartementViewSource}"/></ComboBox.ItemsSource></ComboBox><Label x:Name="labQ9" Foreground="AliceBlue" Style="{StaticResource LabelOnTab}"  Content="Q9. Arrondissement:" Height="28" Margin="10,56,226,134"/><ComboBox x:Name="q9" Style="{StaticResource InputOnTab}" HorizontalAlignment="Left" Height="23" Margin="160,56,0,0" VerticalAlignment="Top" Width="202" IsReadOnly="True" DisplayMemberPath="Libelle" SelectionChanged="q9_SelectionChanged"><ComboBox.ItemsSource><Binding Source="{StaticResource ArrondissementParCommuneViewSource}"/></ComboBox.ItemsSource></ComboBox>

and in VB.NET

Imports System.Data

Class MainWindow

  Dim departementId As String = ""
  Dim communeId As String = ""
  Dim arrondissementId As String = ""

  Private Sub Snigdoffe_Loaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded
    Dim SNIGDoFFEDataSet As SNIGDoFFE.SNIGDoFFEDataSet = CType(Me.FindResource("SNIGDoFFEDataSet"), SNIGDoFFE.SNIGDoFFEDataSet)

    'Load data into the table Departement. You can modify this code as needed.
    Dim SNIGDoFFEDataSetDepartementTableAdapter As SNIGDoFFE.SNIGDoFFEDataSetTableAdapters.DepartementTableAdapter = New SNIGDoFFE.SNIGDoFFEDataSetTableAdapters.DepartementTableAdapter()
    SNIGDoFFEDataSetDepartementTableAdapter.Fill(SNIGDoFFEDataSet.Departement)
    Dim DepartementViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("DepartementViewSource"), System.Windows.Data.CollectionViewSource)
  End Sub

  'Loading of COMMUNES by DEPARTEMENT
  Private Sub q7_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
    Dim SNIGDoFFEDataSet As SNIGDoFFE.SNIGDoFFEDataSet = CType(Me.FindResource("SNIGDoFFEDataSet"), SNIGDoFFE.SNIGDoFFEDataSet)

    Dim drv As System.Data.DataRowView = CType(q7.SelectedItem, DataRowView)
    departementId = drv("Id").ToString() 'I got the Id of the Departement selected                
    Dim SNIGDoFFEDataSetCommuneParDepartementTableAdapter As SNIGDoFFE.SNIGDoFFEDataSetTableAdapters.CommuneTableAdapter = New SNIGDoFFE.SNIGDoFFEDataSetTableAdapters.CommuneTableAdapter()

    'an select * where IdDepartement = ?'  to just get the communes of the departement selected made by QueryBuilder in SNIGDOFFESataSet.xsd
    SNIGDoFFEDataSetCommuneParDepartementTableAdapter.FillByDepartement(SNIGDoFFEDataSet.Commune, departementId)
    Dim CommuneParDepartementViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("CommuneParDepartementViewSource"), System.Windows.Data.CollectionViewSource)
  End Sub

  'Loading of ARRONDISSEMENT by COMMUNE
  Private Sub q8_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
    Dim SNIGDoFFEDataSet As SNIGDoFFE.SNIGDoFFEDataSet = CType(Me.FindResource("SNIGDoFFEDataSet"), SNIGDoFFE.SNIGDoFFEDataSet)

    Dim drv As System.Data.DataRowView = CType(q8.SelectedItem, DataRowView)
    communeId = drv("Id").ToString()
    Dim SNIGDoFFEDataSetArrondissementParCommuneTableAdapter As SNIGDoFFE.SNIGDoFFEDataSetTableAdapters.ArrondissementTableAdapter = New SNIGDoFFE.SNIGDoFFEDataSetTableAdapters.ArrondissementTableAdapter()
    SNIGDoFFEDataSetArrondissementParCommuneTableAdapter.FillByCommune(SNIGDoFFEDataSet.Arrondissement, communeId)
    Dim ArrondissementParCommuneViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("ArrondissementParCommuneViewSource"), System.Windows.Data.CollectionViewSource)
  End Sub    

  'just get the arrondissementId
  Private Sub q9_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
  
    Dim drv As System.Data.DataRowView = CType(q10.SelectedItem, DataRowView)
    arrondissementId = drv("Id").ToString() 
  End Sub

End Class

When I Start Debugging, I can notice that all the exceptions occurred on these lines of :

Dim drv As System.Data.DataRowView = CType(qx.SelectedItem, DataRowView)

Where SelectedItem content is Nothing and drv Content is Nothing.

If you can help please don't hesitate.

Thanks in advance


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>