Not appear the selected items in textBox

Viewed 50

I want to retrieve the object information in my textBox. In my case I want to retrieve the movie details from database, and when I click on title of movie I want to display every information from my SSMS and put in the textBox. I managed to get the movie title and put it in the listBox, but when I click on a title nothing appear in textBox. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataBase
{
public partial class Form1 : Form
{
    DataTable dt = new DataTable();
    public void LoadData()
    {
        new SqlDataAdapter("SELECT * FROM movie", @"Data Source=DESKTOP-UUR5DET\\SQLEXPRESS;Initial 
        Catalog=online_tv;Integrated Security=SSPI;").Fill(dt);
    }

    public class MyMovie
    {
        public int id;
        public string title;
        public override string ToString()
        {
            return title;
        }
    }

    public void ShowMovies()
    {
        int i;
        for (i = 0; i < dt.Rows.Count; i++)
        {
            MyMovie movie = new MyMovie();
            
            movie.title = Convert.ToString(dt.Rows[i]["movie_title"]);

            listBox1.Items.Add(movieBindingSource);
        }
    }

    public Form1()
    {
        InitializeComponent();
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        LoadData();
        ShowMovies();
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = listBox1.SelectedIndex;

        string info = "";
        int i;
        if (dt.Rows.Count > 0 && index >= 0)
        {
            for (i = 0; i < dt.Columns.Count; i++)
            {
                info += dt.Columns[i].ColumnName + ": " +
                    dt.Rows[index][dt.Columns[i].ColumnName] + "\r\n";
            }
        }

        textBox1.Text = info;
    }

    private void Form1_Load_1(object sender, EventArgs e)
    {
       
        this.movieTableAdapter.Fill(this.online_tvDataSet1.movie);

    }
}
}

Here's how movie table looks like: enter image description here

2 Answers

The following line is the issue:

listBox1.Items.Add(movieBindingSource);

You're adding the movieBindingSource to the listBox1 items, but what you need to do instead is add the newly created movie object. Like so:

public void ShowMovies()
{
    int i;
    for (i = 0; i < dt.Rows.Count; i++)
    {
        MyMovie movie = new MyMovie();
        
        movie.title = Convert.ToString(dt.Rows[i]["movie_title"]);

        listBox1.Items.Add(movie);
    }
}

Tested in VS2019, it fills the textBox1 data with the selected row :)

You can use LINQ to query the datatable, and don't need to define MyMovie class.

First, to show Movie Title in listbox, modify ShowMovies as followed.

public void ShowMovies()
{
    int i;
    for (i = 0; i < dt.Rows.Count; i++)
    {
        listBox1.Items.Add(dt.Rows[i][1]); // Title column
    }
}

Then, use LINQ in listBox1_SelectedIndexChanged to get the corresponding Movie info.

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var query = from p in dt.AsEnumerable()
                where p.Field<string>("Title") == ((ListBox)sender).Text
                select new
                {
                    id = p.Field<int>("Id"),
                    title = p.Field<string>("Title"),
                    name = p.Field<string>("Name")
                };

    textBox1.Text = "Id:" + query.FirstOrDefault().id + "\r\n"
                    + "Title:" + query.FirstOrDefault().title + "\r\n"
                    + "Name:" + query.FirstOrDefault().name;
}
Related