1

I made a connection to my database, but when I try to view some data from 1 table, is not showing in dataGridView. When I am pressing the button, few seconds seems like is going to show the values, and after that nothing posted in dataGridView

Can anyone explain if I made sth wrong in my code? I am working in Visual Studio 2013

My code:

private void button1_Click(object sender, EventArgs e)
{
    string connectionString = "datasource=localhost;database=microweb;port=3306;username=root;password=pass";

    MySqlConnection conn = new MySqlConnection(connectionString);
    MySqlCommand cmd = new MySqlCommand("select * from reservations", conn);

    try
    {
        MySqlDataAdapter sda = new MySqlDataAdapter();
        sda.SelectCommand = cmd;
        DataTable dta = new DataTable();
        sda.Fill(dta);
        BindingSource bdsour = new BindingSource();

        bdsour.DataSource = dta;
        dataGridView1.DataSource = bdsour;
        sda.Update(dta);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }    
}
6
  • Is your datasource really named "localhost"? If so, how many (any?) records are contained in dta after the Fill? Commented Mar 17, 2016 at 22:45
  • yes, is named localhost. I made some changes in the connectionString, like another database name, pass, datasource, and is showing me some errors, like there is no database named... or password is incorrect... Commented Mar 17, 2016 at 22:51
  • I tried to use without BindingSource, and no changes.... Commented Mar 17, 2016 at 22:51
  • System.DataCommon.DbDataAdapter.Fill returned 4 Commented Mar 17, 2016 at 22:54
  • Did you create Columns for your datagrid? Commented Mar 17, 2016 at 22:55

1 Answer 1

2

You should set AutoGenerateColumns to true

bdsour.DataSource = dta;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = bdsour;
Sign up to request clarification or add additional context in comments.

1 Comment

@AlexV Glad to have been of help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.