|
-
March 8th, 2008, 12:08 PM
#1
How do i load mysql data into a dataset then into a datagrid?
Hello,
I have been trying for days to get data from a mysql table into a dataset and then fill a datagrid. How do i do this?
Code:
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "**********";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
data = new dataset();
DataSet1.fill(data);
-
March 8th, 2008, 12:52 PM
#2
Re: How do i load mysql data into a dataset then into a datagrid?
Code:
MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);
adapter.Fill(data);
-
March 8th, 2008, 01:58 PM
#3
Re: How do i load mysql data into a dataset then into a datagrid?
Hello,
I am getting an error -
Heres the code -
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.SetDataBinding(mydataset, "table");
myConnection.Close();
Heres the error -
'System.Windows.Forms.DataGridView' does not contain a definition for 'System' and no extension method 'System' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)
Im not sure what the problem is,
-
March 8th, 2008, 02:58 PM
#4
Re: How do i load mysql data into a dataset then into a datagrid?
wherever the error is, its not in that bit of code you posted. look for something named System in your code. (system is the root namespace of .NET apps, so I'd stay away from naming any classes System).
-
March 8th, 2008, 03:12 PM
#5
Re: How do i load mysql data into a dataset then into a datagrid?
Sorry, its actually the SetDataBinding.
'System.Windows.Forms.DataGridView' does not contain a definition for 'SetDataBinding' and no extension method 'SetDataBinding' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)
-
March 8th, 2008, 03:56 PM
#6
Re: How do i load mysql data into a dataset then into a datagrid?
You need to include the namespace that holds the DataGridView class. Look up MSDN for information on DataGridView class.
-
March 8th, 2008, 04:13 PM
#7
Re: How do i load mysql data into a dataset then into a datagrid?
Yes, but MSDN says the DataGridView is held in the system.windows.forms assembly. Which is already included in my program.
http://msdn2.microsoft.com/en-us/lib...agridview.aspx
-
March 8th, 2008, 04:28 PM
#8
Re: How do i load mysql data into a dataset then into a datagrid?
 Originally Posted by PHPRO
Sorry, its actually the SetDataBinding.
'System.Windows.Forms.DataGridView' does not contain a definition for 'SetDataBinding' and no extension method 'SetDataBinding' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)
The message clearly says that SetDataBinding is not a member of DataGridView class. Have you written any extension method called SetDataBinding? If not then this is not the correct way of binding a Dataset to a datagridview. You should go through the documentation shown on MSDN.
 Originally Posted by mariocatch
You need to include the namespace that holds the DataGridView class. Look up MSDN for information on DataGridView class.
SetDataBinding is not a member of datagridview class. There is no namespace missing in that snippet of the code.
-
March 8th, 2008, 04:43 PM
#9
Re: How do i load mysql data into a dataset then into a datagrid?
Try it this way...
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();
-
March 8th, 2008, 04:45 PM
#10
Re: How do i load mysql data into a dataset then into a datagrid?
Yes, thats right. I got mixed up between DataGrid and DataGridView.
Here is the code that runs without errors.
Code:
DataSet mydataset = new DataSet();
DataGrid dataGrid1 = new DataGrid();
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");
dataGrid1.SetDataBinding(mydataset, "table");
myConnection.Close();
However, there is nothing on the windows form when i click debug. How do i produce an output?
Also, nelo , your code produces the same result.
Thanks,
Last edited by PHPRO; March 8th, 2008 at 04:49 PM.
-
March 8th, 2008, 04:52 PM
#11
Re: How do i load mysql data into a dataset then into a datagrid?
 Originally Posted by PHPRO
However, there is nothing on the windows form when i click debug. How do i produce an output?
What do you mean? What output are you expecting?
-
March 8th, 2008, 04:54 PM
#12
Re: How do i load mysql data into a dataset then into a datagrid?
-
March 8th, 2008, 05:01 PM
#13
Re: How do i load mysql data into a dataset then into a datagrid?
Have you checked that you actually have the data in the dataset? I haven't used MySQL but I assume that you're able to connect to the database and that your query is correct. So you are currently using the a DataGrid. Have you tried with a DataGridView (which actually is a replacement for the DataGrid)?
-
March 8th, 2008, 05:30 PM
#14
Re: How do i load mysql data into a dataset then into a datagrid?
Im not sure how to check that the data is in the dataset, but, the connection is correct and the query is correct.
I tried the code you posted for the dataGridView, the result is the same as i am getting for the DataGrid.
-
March 8th, 2008, 06:55 PM
#15
Re: How do i load mysql data into a dataset then into a datagrid?
 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.
I tried the code you posted for the dataGridView, the result is the same as i am getting for the DataGrid.
Have you placed the datagrid/datagridview on your form? debug through the code and see if the dataset is being properly filled.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|