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);
}
}
}
