Hai. I'm having a problem of inserting data to database of mysql. the problem said that the object reference does not set to instance of object. However, I do set it. Here is the code
class saveDatabase
{
#region Object Declaration
protected MySqlConnection conn;
protected MySqlCommand command;
protected frmCreateDB dbase = new frmCreateDB();
#endregion
#region Local Variables
protected int i;
protected string connStr = string.Empty;
protected string database = string.Empty;
#endregion
#region Constructor
public saveDatabase(frmMain Main)
{
database = dbase.getModelName;
connStr = "datasource=localhost;port=3306;username=root;password=root;";
conn = new MySqlConnection(connStr);
command = conn.CreateCommand();
}
#endregion
}
class saveMetabolites : saveDatabase
{
public saveMetabolites(frmMain Main)
: base(Main)
{
conn.Open();
for (i = 0; i < Main.dbMetName.Rows.Count; i++)
{
command.CommandText = "INSERT INTO " + database +
".Metabolites " +
"(MetaboliteID, Metabolite_Name)" +
"VALUES " +
"('" + Main.dbMetName.Rows[i].Cells[0].Value.ToString() +
"' , '" + Main.dbMetName.Rows[i].Cells[1].Value.ToString() + "')";
command.ExecuteNonQuery();
}
conn.Close();
conn.Open();
for (i = 0; i < Main.dbDynamicEq.Rows.Count; i++)
{
command.CommandText = "UPDATE " + database +
".Metabolites " +
"SET DynamicEq = " +
"'" + Main.dbDynamicEq.Rows[i].Cells[1].Value.ToString() + "'" +
"WHERE Metabolite_Name = " +
"'" + Main.dbMetName.Rows[i].Cells[1].Value.ToString() + "'";
command.ExecuteNonQuery();
command.CommandText = "UPDATE " + database +
".Metabolites " +
"SET Initial_Condition = " +
"'" + Main.dbDynamicEq.Rows[i].Cells[2].Value.ToString() + "'" +
"WHERE Metabolite_Name = " +
"'" + Main.dbMetName.Rows[i].Cells[1].Value.ToString() + "'";
command.ExecuteNonQuery();
}
conn.Close();
}
}
class saveCoMetabolites : saveDatabase
{
public saveCoMetabolites(frmMain Main)
: base(Main)
{
conn.Open();
for (i = 0; i < Main.dbCoMetName.Rows.Count; i++)
{
command.CommandText = "INSERT INTO " + database +
".CoMetabolites " +
"(CoMetaboliteID, CoMetabolite_Name)" +
"VALUES " +
"('" + Main.dbCoMetName.Rows[i].Cells[0].Value.ToString() +
"' , '" + Main.dbCoMetName.Rows[i].Cells[1].Value.ToString() + "')";
command.ExecuteNonQuery();
}
conn.Close();
}
}
class saveKineticEq : saveDatabase
{
public saveKineticEq(frmMain Main)
: base(Main)
{
MessageBox.Show(database.ToString());
conn.Open();
for (i = 0; i < Main.dbKinetEq.Rows.Count; i++)
{
command.CommandText = "INSERT INTO " + database +
".EnzymeKinetics " +
"(Kinetic_Name, Equations)" +
"VALUES " +
"('" + Main.dbKinetEq.Rows[i].Cells[0].Value.ToString() +
"' , '" + Main.dbKinetEq.Rows[i].Cells[1].Value.ToString() + "')";
command.ExecuteNonQuery();
}
conn.Close();
}
}
class saveKineticPar : saveDatabase
{
public saveKineticPar(frmMain Main)
: base(Main)
{
conn.Open();
for (i = 0; i < Main.dbKinetPar.Rows.Count; i++)
{
command.CommandText = "INSERT INTO " + database +
".KineticParameters " +
"(Parameter_Name, Parameter_Value)" +
"VALUES " +
"('" + Main.dbKinetPar.Rows[i].Cells[0].Value.ToString() +
"' , '" + Main.dbKinetPar.Rows[i].Cells[1].Value.ToString() + "')";
command.ExecuteNonQuery();
}
conn.Close();
}
}
In the main class, i defined the object like this
saveDatabase svdb;
and inside function of class i do it like this
private bool tryClose(FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Save Changes?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
switch (result)
{
case DialogResult.Yes:
SaveAs();
return false;
case DialogResult.No:
return false;
default:
e.Cancel = true;
break;
}
return true;
}
private void SaveAs()
{
if (frmCreateDb.getModelName == string.Empty)
{
if (frmCreateDb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
db.CreateDatabase();
MessageBox.Show("New Project is successfully created");
svdb = new saveMetabolites(this);
svdb = new saveCoMetabolites(this);
svdb = new saveKineticEq(this);
svdb = new saveKineticPar(this);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
else
{
try
{
svdb = new saveMetabolites(this);
svdb = new saveCoMetabolites(this);
svdb = new saveKineticEq(this);
svdb = new saveKineticPar(this);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}