Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im a beginner in c#.net. I'm having problem in binding the database (mysql) to datagridview. Here is my code

Form that creating the database which I do not have any problem.

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using MySql.Data;
using MySql.Data.MySqlClient;
 
namespace DynamicSimulator_v2
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
 
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private void btnNew_Click(object sender, EventArgs e)
        {
            SchemaName schemaForm = new SchemaName();
            frmMetCon form2 = new frmMetCon();
 
            if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
              string connStr = "datasource=localhost;port=3306;username=root;password=root;";
              string database = schemaForm.getData();
              MySqlConnection conn = new MySqlConnection(connStr);
                try
                 {
                   MySqlCommand command = conn.CreateCommand();
                   conn.Open();
                   command.CommandText = "DROP DATABASE IF EXISTS " + database;
                   command.ExecuteNonQuery();
                   command.CommandText = "CREATE DATABASE " + database;
                   command.ExecuteNonQuery();
                   command.CommandText = "CREATE TABLE " +database+
                                         ".Metabolites("+
                                         "MetaboliteID VARCHAR(10) NOT NULL,"+
                                         "Metabolite_Name VARCHAR(45) NULL," +
                                         "ReactionTime INT NULL, " + 
                                         "PRIMARY KEY (MetaboliteID));";
                   command.ExecuteNonQuery();
                   command.CommandText = "INSERT INTO " + database +
                                         ".Metabolites " +
                                         "(MetaboliteID)" +
                                         "VALUES " +
                                         "('met1');";
                   command.ExecuteNonQuery();
 
                   command.CommandText = "INSERT INTO " + database +
                                         ".Metabolites " +
                                         "(MetaboliteID)" +
                                         "VALUES " +
                                         "('met2');";
                   command.ExecuteNonQuery();
 
                   command.CommandText = "INSERT INTO " + database +
                                         ".Metabolites " +
                                         "(MetaboliteID)" +
                                         "VALUES " +
                                         "('met3');";
                   command.ExecuteNonQuery();
                   this.Hide();
                   form2.Show();   
                 }
                 catch (Exception ex)
                 {
                   MessageBox.Show(ex.Message); 
                 }
                 conn.Close();
           
             }
        }
    }
}


Form that I'm having a problem

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using MySql.Data;
using MySql.Data.MySqlClient;
 
namespace DynamicSimulator_v2
{
    public partial class frmMetCon : Form
    {
        public frmMetCon()
        {
            InitializeComponent();
        }
 
        private void cmbMet_SelectedIndexChanged(object sender, EventArgs e)
        {
            string connStr = "datasource=localhost;port=3306;username=root;password=root;";
            MySqlConnection conn = new MySqlConnection(connStr);
            bool hasSelection = (cmbMet.SelectedIndex >= 0);
            SchemaName schemaForm = new SchemaName();
            string database = schemaForm.getData();
            if (hasSelection == true)
            {
             grpMetName.Visible = true;
                try
                {
                  string command;
                  MySqlDataAdapter sqlData = new MySqlDataAdapter();
                  DataTable dtable = new DataTable();
                  if (cmbMet.SelectedIndex == 2)
                   {
                    conn.Open();
                    command = "SELECT Metabolite_Name" +
                              "FROM " + database +
                              ".Metabolites " +
                              "WHERE MetaboliteID IN ('met1', 'met2')";
                    sqlData = new MySqlDataAdapter(command, conn);
                    sqlData.Fill(dtable);
                    dbMetName.DataSource = dtable;
                    
                   }
                  else if (cmbMet.SelectedIndex == 3)
                   {
                    conn.Open();
                    command = "SELECT Metabolite_Name" +
                              "FROM " + database +
                              ".Metabolites " +
                              "WHERE MetaboliteID IN ('met1', 'met2', 'met3')";
                    sqlData = new MySqlDataAdapter(command, conn);
                    sqlData.Fill(dtable);
                    dbMetName.DataSource = dtable;
                    
                   }
  
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                conn.Close();
            }
       }
  }
}


Form that user input and passing the database name

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DynamicSimulator_v2
{
    public partial class SchemaName : Form
    {
        private string data;

        public SchemaName()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            data=txtDB.Text;
            this.Hide();
        }

        public string getData()
        {
            return data;
        }

    }
}


the datagridview is dbMetName. The datagridview does not shows anything - just a plain grey panel
Posted
Comments
thatraja 19-Oct-13 8:43am    
If cmbMet.SelectedIndex is not 2 or 3 then it won't bind data(based on your code), while debugging, did the debugger go in to that if else condition?
arave0521 19-Oct-13 9:06am    
yes, it goes to the condition but the datagridview only shows plain grey panel

The code looks good to me.

Did you put a break point on line
Quote:
dbMetName.DataSource = dtable;


and check whether you have any records in your datatable ?
 
Share this answer
 
v2
Comments
arave0521 20-Oct-13 11:04am    
hai, i don't get it where should i put the break points. i thought if we put
<pre> break; </pre>

it resulting error in if else statements
Refer this article to know how to put a breakpoint.

http://msdn.microsoft.com/en-us/library/ktf38f66(v=vs.90).aspx[^]

An easy way to do in VS is just click on the left bar, you will come to know how to do when you try the above option
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900