Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
        {
            //Insert Metabolites Values
            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();

            //Insert Dynamic Equations Value
            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)
        {
            //Insert Co-Metabolites Values
            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)
        {
            //Insert Kinetic Equations Values
            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)
        {
            //Insert Kinetic Parameters Values
            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

C#
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;
       }


C#
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);
               }
           }
       }
Posted
Comments
DamithSL 9-May-14 0:20am    
which line you get this error?
arave0521 9-May-14 0:39am    
when I do breakpoint, I found it it the problem because of command.ExecuteNonQuery();inside for looping. It said object reference is not set to instance.
this part of code

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

where all of declaration of object and variable are made inside constructor. and global part

You're apparently expecting us to guess which line this error happens on?

The error is really easy to diagnose. Run your app under the debugger. When the error shows up, it'll be pointing directly at the line that threw it. User the debugger to look at all of the variables on that line and find the one that returns null. Your code is making the assumption that an object was returned by some piece of code when no object was returned. You are then trying to either call a method on that object (null) or get/set a property on it. Since you cannot do that on an object that doesn't exist, you get the error you're seeing.

It's up to you to look backwards through the code and figure out why you're getting a null back for an object instead of what you're expecting.
 
Share this answer
 
u hav not instantiated command with new keyword
Try this
protected MySqlCommand command=new MySqlCommand()
This may solve your issue
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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