1

I have a MySQL connection in C# that supposedly puts the table data into a DataGridView, but it has a problem and it makes the rows for every account in my users table in my db, but it doesn't show the actual data. Can anyone help?

My code:

public Form1()
{
    InitializeComponent();

    string connectionString = "Server=mysql.dunkycart.com; Database=dunkycart; Uid=dunkycart; Pwd=rooB-dnK-sqL;"; //Set your MySQL connection string here.
    string query = "SELECT name, username, email FROM users"; // set query to fetch data "Select * from  tabelname"; 
    using (MySqlConnection conn = new MySqlConnection(connectionString))
    {
        using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
        {
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
}

The issue: This is the output of the form. I entered the column names manually, btw, so it's not doing that either.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • You may find this post useful. [Is there a way to auto generate controls on a form from a binding source?](http://stackoverflow.com/questions/38165043/is-there-a-way-to-auto-generate-controls-on-a-form-from-a-binding-source) – Reza Aghaei Oct 26 '16 at 17:29

2 Answers2

0

Because you are using DataSet instead of DataTable so you need to change this:

adapter.Fill(ds);

To this:

adapter.Fill(ds,"tbl");

Or you can use a DataTable instead of DataSet:

DataTable dt = new DataTable();
adapter.Fill(dt);
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

Nevermind, I just found a fix. What I had to do is set the DataPropertyName for each column in the DataGridView to the corresponding column in the db table.