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