Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

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

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

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

User generated image
        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

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

I don't understand how to implement the last Experts comments
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.
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
I'm closing this unresolved