Fill ComboBox with Results of LINQ Query, Directly

11,514

Try to fill the companyComboBox after the initializeComponent

 InitializeComponent();
    companyComboBox.ItemsSource = companyQuery.ToList();
    companyComboBox.DisplayMemberPath = "Name";
    companyComboBox.SelectedValuePath = "ID";
Share:
11,514
Dan.
Author by

Dan.

Updated on June 07, 2022

Comments

  • Dan.
    Dan. almost 2 years

    I am new to c#/.net/WPF.

    I am trying to fill a combobox with values taken from a database.

    The LINQ query gets a list of all companies in the database and the code attempts to fill a ComboBox control with this list.

    The C# code below successfully gets the results (I previously outputted it with a MessageBox.Show()).

    My Next step was to remove that bit and, instead, put in the code which would fill out this ComboBox:

    <ComboBox Name="companyComboBox"/>
    

    The c#:

        using System;
        using System.Collections;
        using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Data.SqlClient;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    
    namespace LeadSystem
    {
        /// <summary>
        /// Interaction logic for NewLead.xaml
        /// </summary>
        public partial class NewLead : Window
        {
    
        public NewLead()
        {
    
    
           // Use a connection string.
            DataContext db = new DataContext("Data Source=HP\\SQLEXPRESS;Initial Catalog=LeadSystem;Integrated Security=True");
    
            // Get a typed table to run queries.
            Table<Company> Companies = db.GetTable<Company>();
    
            // Attach the log to show generated SQL.
            db.Log = Console.Out;
    
            // Query for all companies.
            var companyQuery = 
                from c in Companies
                select new { Name = c.CompanyName, ID = c.CompanyID };
    
            companyComboBox.ItemsSource = companyQuery.ToList();
            companyComboBox.DisplayMemberPath = "Name";
            companyComboBox.SelectedValuePath = "ID";
            InitializeComponent();
    
        }
    }
    }
    

    The problem I keep getting is:

    Object reference not set to an instance of an object.
    

    ^ it's talking about companyQuery, where I try to use it to fill the comboBox.

    I thought this must be because of deferred execution, and so I had a look around the web to find a solution. I've seen several people say to add ToList() at the end of that line of code, but nothing changed.

    So, does someone here know what I'm doing wrong??

    I have looked around the web (including Stackoverflow) and nothing has helped me fix mine.

    Also, if it's not too cheeky to ask two questions in one go... How do I set the selected value and displayed values in my ComboBox? Is it correct, the way I already have it?

    Thanks