I'm new to C# and trying to show data from Mysql database to ListView, but keep on failing.
by these coding i didnt get any error but no data showing at the listview when i run the program.
Please advice and help, thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace sql_to_listview
{
public partial class Form1 : Form
{
MySqlConnection cn = new MySqlConnection("Data Source=localhost;Initial
Catalog=rfiddb;uid=username;pwd=password;");
MySqlCommand cmd = new MySqlCommand();
MySqlDataReader dr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cn.Open();
cmd.CommandText = "select * from data";
cmd.Connection = cn;
dr = cmd.ExecuteReader();
while(dr.Read())
{
ListViewItem lv = new ListViewItem(dr[0].ToString());
lv.SubItems.Add(dr[1].ToString());
lv.SubItems.Add(dr[2].ToString());
lv.SubItems.Add(dr[3].ToString());
listView1.Items.Add(lv);
}
cn.Close();
}
}
}
Here is the data table looks like