title: How to bind combobox data from database using wpf and xaml?
body: <p>I am trying to bind data to my <code>ComboBox</code> from a database table. I followed this tutorial <a href="http://blog.cylewitruk.com/2010/09/wpf-combobox-and-databinding-datacontext-itemssource-displaymemberpath-selecteditem-selectedvalue-selectedvaluepath/" rel="nofollow">http://blog.cylewitruk.com/2010/09/wpf-combobox-and-databinding-datacontext-itemssource-displaymemberpath-selecteditem-selectedvalue-selectedvaluepath/</a> but he only binds the data by manually adding the data. </p>
  
  <p>When I try to add a method for fetching data from the database, my Window.DataContext will give below error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>Where am I supposed to add the code to populate my <code>ComboBox</code>?</p>
  
  <p>This is my xaml file.</p>
  
  <pre><code>&lt;Window 
  x:Name="windowMain" 
  x:Class="QSMSystemView.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Quickserve Marketing Inventory and Accouting System" Height="437" Width="499"
  xmlns:ViewModels="clr-namespace:QSMSystemView"&gt;
  
  &lt;Window.DataContext&gt;
      &lt;ViewModels:ProductViewModel /&gt;
  &lt;/Window.DataContext&gt;
  ...
  
  &lt;ComboBox x:Name="comboBoxSupplier" 
            ItemsSource="{Binding Suppliers, Mode=OneWay}"
            DisplayMemberPath="Name"
            SelectedValue="{Binding SelectedProductSupplier}"
            SelectedValuePath="ID"
            /&gt;
  </code></pre>
  
  <p>This is my viewmodel:</p>
  
  <pre><code>public class ProductViewModel : ViewModel
  {
  
      private SuppliersModel suppliersModel;
  
      public ObservableCollection&lt;Supplier&gt; Suppliers { get; set; }
  
      private Int16 selectedProductSupplier;
  
      public Int16 SelectedProductSupplier
      {
          get { return this.selectedProductSupplier; }
          set
          {
              this.selectedProductSupplier = value;
  
              this.NotifyPropertyChanged("SelectedProductSupplier");
          }
      }
  
      public ProductViewModel()
      {
          this.Suppliers = new ObservableCollection&lt;Supplier&gt;();
          this.suppliersModel = new SuppliersModel();
          this.GetSuppliersFromDatabase();
      }
  
      public void GetSuppliersFromDatabase()
      {
          foreach (Supplier supplier in suppliersModel.All())
          {
              Console.WriteLine("Id: " + supplier.ID);
              Console.WriteLine("name: " + supplier.Name);
              this.Suppliers.Add(supplier);
          }
      }
  
  }
  </code></pre>
  
  <p>This is my Supplier class.</p>
  
  <pre><code>public class Supplier
  {
      public Int16 ID { get; set; }
      public String Name { get; set; }
  
      public Supplier(Int16 id, String name)
      {
          this.ID = id;
          this.Name = name;
      }
  }
  </code></pre>
  
  <p>Note: If I don't try to fetch data from the database and just manually populate the Supplier by:
      this.Supplier.Add(new Supplier(1, "foo");
  then my code will run with no errors.</p>