C# How to populate a listbox from an SQL query

Hi

I'm new to C#

I'm trying to populate a listbox from an database query  the idea is to be able to click a movie title and then fetch other info about that movie using the movie id

I've connected to the SQLite database (See Pic) and added a listbox named TitleListbox to my MainWindow.xaml

Now I'm stuck
How do I populate the TitleListbox?

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            GetTitles();  

        }

        private void GetTitles(ListBox TitleListBox)
        {
            TitleListBox.Items.Clear();
            MovieDatabaseDataSet ds = new MovieDatabaseDataSet("select id,title,tmdbId from movie order by title");

        }
    }

Open in new window

Movie DB Conection
LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
DataTable dt = ds.Tables[0];
TitleListBox.DataSource =  dt;
TitleListBox.DisplayMember = "title";
TitleListBox.ValueMember = "id";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevor1940Author Commented:
Hi  sorry I still have red line under new MovieDatabaseDataSet("select id,title,tmdbId from movie order by title");
it say MovieDatabaseDataSet dose not contain a constructor that takes 1 argument

VBErrorClipboard01.jpg
        public MainWindow()
        {
            InitializeComponent();

            GetTitles();  

        }

        private void GetTitles(ListBox TitleListBox)
        {
            TitleListBox.Items.Clear();
            MovieDatabaseDataSet ds = new MovieDatabaseDataSet("select id,title,tmdbId from movie order by title");
            DataTable dt = ds.Tables[0];
            TitleListBox.DataSource = dt;
            TitleListBox.DisplayMember = "title";
            TitleListBox.ValueMember = "id";

        }
    }

Open in new window

trevor1940Author Commented:
Can some one show me the code how to do this?

I don't understand how to implement the last Experts comments
SolarWinds® IP Control Bundle (IPCB)

Combines SolarWinds IP Address Manager and User Device Tracker to help detect IP conflicts, quickly identify affected systems, and help your team take near instantaneous action. Help improve visibility and enhance reliability with SolarWinds IP Control Bundle.

Kyle AbrahamsSenior .Net DeveloperCommented:
You have to populate the data set from the XSD.  I thought you already had that portion done.
You have to do 2 things:  
1) Create the structure from the dataset:
from: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/loading-dataset-schema-information-from-xml

DataSet dataSet = new DataSet();  
dataSet.ReadXmlSchema("schema.xsd");  

Open in new window


and
2: Populate the dataset:
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/loading-a-dataset-from-xml

string xmlData = "<XmlDS><table1><col1>Value1</col1></table1><table1><col1>Value2</col1></table1></XmlDS>";  // change to your real data, can read from a file or whatever.

System.IO.StringReader xmlSR = new System.IO.StringReader(xmlData);  

dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema);  

Open in new window


from there you can run the select on the table as shown.
trevor1940Author Commented:
Now you've totally lost me

The data comes from an SQL query why do I need to create an XML file?

All I'm trying to do is populate a list box with film titles (this Works)
When user  Clicks a title I want to get the film ID not the selected index
trevor1940Author Commented:
I'm closing this unresolved
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.