Filling WPF Combobox with SQL Column
<p>Going off of this thread: <a href="https://stackoverflow.com/questions/7867647/attach-a-sql-database-to-combobox-itemsource-wpf">Attach a SQL database to ComboBox.ItemSource (WPF)</a></p>

<p>I am still confused on how to execute. Below is my code for my GUI. I am new to C# and am unsure how to manipulate the combo box. </p>

<p>I am trying to take data from a SQL query and fill the values of a combobox. </p>

<pre><code>using System;
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.Navigation;
using System.Windows.Shapes;

namespace List
{
    /// &lt;summary&gt;
    /// Interaction logic for MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var instance = new SQLInformation();
            instance.fillComboBox("Data Source=server Initial Catalog=db; User id=user; Password=pass;", System.Windows.Controls.ComboBox. , "select distinct [location] from [dbo].[locations]", null, "[location]");
        }
    }

}
</code></pre>

<p>SQL Information code:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Specialized;
using System.Configuration;


namespace List
{
    public class SQLInformation
    {
        public bool fillComboBox(string connectionString, System.Windows.Controls.ComboBox combobox, string query, string defaultValue, string itemText)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter sqladp = new SqlDataAdapter();
            DataSet ds = new DataSet();


            try
            {
                using (SqlConnection _sqlconTeam = new SqlConnection(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString))
                {
                    sqlcmd.Connection = _sqlconTeam;
                    sqlcmd.CommandType = CommandType.Text;
                    sqlcmd.CommandText = query;
                    _sqlconTeam.Open();
                    sqladp.SelectCommand = sqlcmd;
                    sqladp.Fill(ds, "defaultTable");
                    DataRow nRow = ds.Tables["defaultTable"].NewRow();
                    nRow[itemText] = defaultValue;
                    ds.Tables["defaultTable"].Rows.InsertAt(nRow, 0);
                    combobox.DataContext = ds.Tables["defaultTable"].DefaultView;

                    combobox.DisplayMemberPath = ds.Tables["defaultTable"].Columns[0].ToString();
                    combobox.SelectedValuePath = ds.Tables["defaultTable"].Columns[1].ToString();
                }
                return true;
            }
            catch (Exception expmsg)
            {
                return false;
            }
            finally
            {
                sqladp.Dispose();
                sqlcmd.Dispose();
            }
        }
    }
}
</code></pre>

<p>Here is my XAML code:</p>

<pre><code>&lt;Window x:Class="List.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FirstWindow" Height="390" Width="683"&gt;
    &lt;Window.Background&gt;
        &lt;LinearGradientBrush StartPoint='0,0' EndPoint='0,1'&gt;
            &lt;LinearGradientBrush.GradientStops&gt;
                &lt;GradientStop Color='#FFC1C1C1' Offset="0.99" /&gt;
                &lt;GradientStop Color='White' /&gt;
                &lt;GradientStop Color="#FFE4E4E4" Offset="0.397"/&gt;
                &lt;GradientStop Color="#FFD1D1D1" Offset="0.777"/&gt;
            &lt;/LinearGradientBrush.GradientStops&gt;
        &lt;/LinearGradientBrush&gt;
    &lt;/Window.Background&gt;
    &lt;Grid&gt;        
        &lt;Grid Height="360" VerticalAlignment="Top"&gt;
            &lt;Image HorizontalAlignment="Left" Height="50" Margin="119,10,0,0" VerticalAlignment="Top" Width="270" Source=""/&gt;
            &lt;Image HorizontalAlignment="Left" Height="23" Margin="153,50,0,0" VerticalAlignment="Top" Width="209" Source=""/&gt;

            &lt;ComboBox x:Name="LocationComboBox" HorizontalAlignment="Left" Margin="153,122,0,0" VerticalAlignment="Top" Width="73" SelectedIndex="0"&gt;

            &lt;/ComboBox&gt;

        &lt;/Grid&gt;

    &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>