CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Feb 2008
    Posts
    22

    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);

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: How do i load mysql data into a dataset then into a datagrid?

    Code:
    MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);
    adapter.Fill(data);

  3. #3
    Join Date
    Feb 2008
    Posts
    22

    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,

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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).

  5. #5
    Join Date
    Feb 2008
    Posts
    22

    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?)

  6. #6
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    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.

  7. #7
    Join Date
    Feb 2008
    Posts
    22

    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

  8. #8
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How do i load mysql data into a dataset then into a datagrid?

    Quote 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.
    Quote 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.

  9. #9
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Resolved 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();

  10. #10
    Join Date
    Feb 2008
    Posts
    22

    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.

  11. #11
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: How do i load mysql data into a dataset then into a datagrid?

    Quote 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?

  12. #12
    Join Date
    Feb 2008
    Posts
    22

    Re: How do i load mysql data into a dataset then into a datagrid?

    A grid/table of results.

  13. #13
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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)?

  14. #14
    Join Date
    Feb 2008
    Posts
    22

    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.

  15. #15
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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.

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured