Thread: [C#] MySQl database problems

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    [C#] MySQl database problems

    Hello everyone,

    I've a problem with a MySQL database and filling the dataset, the database is running local on XAMPP.

    this is my Class where I set the properties.

    Code:
    namespace SQLDatabaseApp
    {
        class DBSettings
        {
            public void BasicSettings(DataGridView Grid)
            {
                string strConnectionstring = "Data Source=localhost;" + "Initial Catalog=reisbureau;" + "username=root;password=''";
                string strCommandtext = "SELECT * FROM klanten";
    
                MySqlConnection Connection = new MySqlConnection(strConnectionstring);
                MySqlCommand Command = new MySqlCommand(strCommandtext);
                MySqlDataAdapter Adapter = new MySqlDataAdapter();
                DataSet ds = new DataSet();
    
                Connection.Open();
                Adapter.SelectCommand = Command;
                Adapter.Fill(ds);
    
            }
    
    
        }
    }
    this is my Form.cs Code
    Code:
    namespace SQLDatabaseApp
    {
        public partial class Form1 : Form
        {
            DBSettings settings = new DBSettings();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                settings.BasicSettings(dgvViewDatabase);
            }
        }
    }
    If i try to run this code is selects the line: "Adapter.Fill(ds);" and report the following error

    Fill: SelectCommand.Connection property has not been initialized.


    Does somebody knows what I am doing wrong?

    thank you for your support.

    Greetings

    Jelte.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    "SelectCommand.Connection property has not been initialized."

    that should tell you everything you need to know.
    either set the connection property of your command to the sqlconnection object, or use CreateCommand() from the sqlconnection object.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you using the MyODBC interface to connect? If so, can I assume you have it set up correctly? If not did you RTFM?

  4. #4
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    No, i use the MySQL .net Connector.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Quote Originally Posted by bling View Post
    "SelectCommand.Connection property has not been initialized."

    that should tell you everything you need to know.
    either set the connection property of your command to the sqlconnection object, or use CreateCommand() from the sqlconnection object.
    QFT

    You failed to supply the second argument to your commands contructor call.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Oke I think I fixed the initialization problem. but if I run the program, no errors or something else shows up, only the Datagridview stays empty. XAMPP is runing and there is no firewall or something like it that blocks the program.

    this is my code:
    Code:
    namespace DBSQLApp
    {
        class DBSettings
        {
            public void BasicSettings(DataGridView grid)
            {
                string strConnectionstring = "Data Source=localhost;" + "Initial Catalog=reisbureau;" + "username=root;password=''";
                string strCommandtext = "SELECT * FROM klanten";
    
                
                MySqlConnection Connection = new MySqlConnection(strConnectionstring);
                MySqlCommand Command = new MySqlCommand(strCommandtext,Connection);
                MySqlDataAdapter Adapter = new MySqlDataAdapter();
                DataSet ds = new DataSet();
    
                Connection.Open();
                Adapter.SelectCommand = Command;
                Adapter.Fill(ds,"klanten");
    
                grid.DataSource = ds;
            }
    Form.cs Code:
    Code:
    namespace DBSQLApp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            DBSettings settings = new DBSettings();
    
            public void Form1_Load(object sender, EventArgs e)
            {
                settings.BasicSettings(Gridview);
            }
        }
    }
    everyone Thank you for your Help.

    Greetings

    Jelte.

    Edit:
    Thanks for all your help people, I fixed the problem:

    the Solution was.

    Grid.Datasource = ds.Tables[0];
    Last edited by Jelte; 09-26-2008 at 04:58 AM. Reason: Fixed the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MySQL C API Header File Problems
    By WarrenKeyes3 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 02:26 PM
  2. literature database: help with planning
    By officedog in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2009, 12:34 PM
  3. Problem connecting to MySQL Database?
    By MrLucky in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2006, 11:30 AM
  4. About C++ and MySQL or oether free database
    By xxxrugby in forum C++ Programming
    Replies: 18
    Last Post: 12-30-2005, 06:28 AM
  5. Connecting to MySQL Database
    By ussj4gohan in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2001, 09:41 AM