Re: How do i load mysql data into a dataset then into a datagrid?
Quote:
Originally Posted by PHPRO
Im not sure how to check that the data is in the dataset, but, the connection is correct and the query is correct.
Are you using Vistual Studio? If so it has some sophisticated debugging environment. You can add your dataset to the watch window and do something like
mydataset["table"].Rows.Count that will tell if there are rows retrieved in the table.
You can also serialize the dataset to a file if you haven't got visual studio development environment to see the contents of the dataset.
You can also produce debugging output using the Debug and Trace classes from System.Diagnostics namespace.
Re: How do i load mysql data into a dataset then into a datagrid?
Hello,
I will tell you what i have done -
In the "Design" view on visual studio i added a DataGridView - VS starts asking for information about the database. MySQL is not supported for the GUI way of adding information to the DataGridView, therefore , the DataGridView must be filled using code - so i ignore the "Add Coloumn" options etc..
I then switch to the code view and add the code nelo posted to the form_load function of my form.
Code:
DataSet mydataset = new DataSet();
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "************";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);
adapter.Fill(mydataset, "table");
dataGridView1.DataSource = mydataset;
dataGridView1.DataMember = "table";
myConnection.Close();
I then click debug, open the form and the DataGridView is just grey with no information. Could the problem be that "mydatasource" was not created in the design view of VS and therefore, does not show in the design view?
The code and connection are 100% correct as i im using the same code in another formI dont understand what you mean nelo,
Thanks,
Re: How do i load mysql data into a dataset then into a datagrid?
set a breakpoint on
dataGridView1.DataSource = mydataset;
(just after the fill method) and look at mydataset (you can hover your mouse over the variable, or right click it and choose quick view. if the dataset it empty, then nothing will be displayed on your grid. try running the query in phpmyadmin and see what results you get.
you can also try using a binding source for the data source (no idea what difference it would make):
Code:
DataTable table = new DataTable();
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "************";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);
adapter.Fill(table, "table");
BindingSource bs = new BindingSource();
bs.DataSource = table;
dataGridView1.DataSource = bs;
myConnection.Close();
Re: How do i load mysql data into a dataset then into a datagrid?
Quote:
Originally Posted by MadHatter
set a breakpoint on
dataGridView1.DataSource = mydataset;
(just after the fill method) and look at mydataset (you can hover your mouse over the variable, or right click it and choose quick view. if the dataset it empty, then nothing will be displayed on your grid. try running the query in phpmyadmin and see what results you get.
I did this and when i hovered over the code it was just explaining the code and telling me what it does not if the dataset contains anything.
I ran the query on phpmyadmin and it worked.
Quote:
you can also try using a binding source for the data source (no idea what difference it would make):
Code:
DataTable table = new DataTable();
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "************";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);
adapter.Fill(table, "table");
BindingSource bs = new BindingSource();
bs.DataSource = table;
dataGridView1.DataSource = bs;
myConnection.Close();
This code did not compile, the error is on adapter.Fill(table, "table");
The best overloaded method match for 'System.Data.Common.DataAdapter.Fill(System.Data.DataTable, System.Data.IDataReader)' has some invalid arguments Argument '2': cannot convert from 'string'
Thanks for your continued help,
Re: How do i load mysql data into a dataset then into a datagrid?
ah, guess it should be:
Code:
adapter.Fill(table);