Click here to Skip to main content
15,896,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai. I'm having problem in Update or insert into mySQL from Visual C#. The situation is that user written inside specific row,column of datagridview and I'm using cell end edit event to insert the data inside database. Here is the code of insert and update.

C#
class UpdateMetabolite: UpdateDatabase
    {
        public UpdateMetabolite(frmMain mainClass)
            : base(mainClass)
        {
            int rowIndex = mainClass.dbMetName.CurrentCell.RowIndex;
            try
            {
                string metNo = mainClass.dbMetName.Rows[rowIndex].Cells[0].Value.ToString();
                string metName = mainClass.dbMetName.Rows[rowIndex].Cells[1].Value.ToString();

                conn.Open();
                if (!string.IsNullOrEmpty(mainClass.dbMetName.Rows[rowIndex].Cells[1].Value.ToString()))
                {
                    command.CommandText = "SELECT COUNT(*) FROM " + database +
                                          ".Metabolites " +
                                          "WHERE MetaboliteID = " +
                                           "'" + metNo + "'";
                    command.ExecuteNonQuery();
                    decimal count = (decimal)command.ExecuteScalar(); //the problem occur here
                    if (count > 0)
                    {
                        command.CommandText = "UPDATE " + database +
                                              ".Metabolites " +
                                              "SET Metabolite_Name = " +
                                              "'" + metName + "'" +
                                              "WHERE MetaboliteID = " +
                                              "'" + metNo + "'";
                        command.ExecuteNonQuery();
                    }
                    else
                    {
                        command.CommandText = "INSERT INTO " + database +
                                              ".Metabolites " +
                                              "(MetaboliteID, Metabolite_Name)" +
                                              "VALUES " +
                                              "('" + metNo + "','" + metName + "');";
                        command.ExecuteNonQuery();
                    }
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }


I want to capture the count before enter if else statement. but the error stated specific cast is not valid. If I declare int count = (int)command.ExecuteNonQuery() before enter if else statement, it returns -1 value no matter what.
Posted

1 solution

Why you are twice executing this query? In addition problem seems that your query returning NULL causing exception. Try this


C#
command.CommandText = "SELECT COUNT(*) FROM " + database +
                                          ".Metabolites " +
                                          "WHERE MetaboliteID = " +
                                           "'" + metNo + "'";
  //command.ExecuteNonQuery();
  object tCount = command.ExecuteScalar(); //the problem occur here</pre>
  int count = 0;
  if (tCount != null)
      count = (int)tCount;
 
Share this answer
 
Comments
arave0521 23-Jun-14 10:23am    
the error still the same. but i tried figure it out. and it works fine. but not sure the algorithm can be considered proper or not. here is my code

command.CommandText = "SELECT COUNT(*) FROM " + database +
".Metabolites " +
"WHERE MetaboliteID = " +
"'" + metNo + "'";
command.ExecuteNonQuery();
int count = int.Parse(command.ExecuteScalar().ToString());

if (count > 0)
{

}
else
{

}

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