title: Tracing error in database
body: <p>I have a client server program and the server checks if the client opened the right application. If not then it will display some warning. It should only open three applications along with the right one. I am planning to put the data of opened application and the users hostname into the database. There will be two table, hostnametable and processtable.
  Example:</p>
  
  <p>hostnametable</p>
  
  <pre><code>hostname_id | hostname
  ------------------------
        1     | Guest1
        2     | Guest2
        3     | Guest3
  </code></pre>
  
  <p>processtable</p>
  
  <pre><code>id | processID | processName | processExe | processPath     | hostname_id
  --------------------------------------------------------------------------
  1  |    4332   |  C# Basic   |  adobe.exe | C:/Users/Adm... |      2      
  2  |    5400   |  PHP 101    |  ppt.exe   | C:/Users/Adm... |      2      
  3  |    0943   |  Notice     |notepad.exe | C:/Users/Adm... |      1      
  4  |    8932   |  Java 101   |  adobe.exe | C:/Users/Adm... |      3      
  5  |    7608   |  Certific   |  word.exe  | C:/Users/Adm... |      1      
  6  |    0101   |  Aliens     |  ppt.exe   | C:/Users/Adm... |      1      
  7  |    8200   |  Progress   |  excel.exe | C:/Users/Adm... |      3   
  </code></pre>
  
  <p>Once the client open a new application, [hostname,processID,processName, processExe, processPath] will be sent in the server. The server will </p>
  
  <blockquote>
    <p>-check if the hostname exist by getting the hostname count,  </p>
    
    <p>-if yes then it will select the hostname_id and  </p>
    
    <p>-insert it in a new table along with the other data.</p>
  </blockquote>
  
  <p>If not </p>
  
  <blockquote>
    <p>-then a new hostname is inserted in the hostname table </p>
    
    <p>-it will select the hostname_id </p>
    
    <p>-and insert the data</p>
  </blockquote>
  
  <p>This is a one-to-many relationship so in the later part I need to retrieve it for output display and validation.
  My problem is I've debugged the database part of the code step by step but when I run them as a whole it will catch an error at the very end of the process. In here it will automatically go to </p>
  
  <pre><code>catch { output.Text += "\nERROR THREE\n "; }
  </code></pre>
  
  <p>Here is my code.</p>
  
  <pre><code> using (MySqlConnection conDataBase = new MySqlConnection(constring))
              {
                  try
                  {
                      if (exeFile == openedFile)
                      {
                          listBox1.ForeColor = System.Drawing.Color.Blue;
                          listBox1.Items.Add(machineName + " has opened the right file.");
                      }
                      else
                      {
                          string count1 = "SELECT COUNT(hostName) FROM hostnametable;";
                          string gethostname_id = "SELECT INTO chtbuster.hostnametable(hostName_id) WHERE hostName=?machineName";
                          string insert_pt = "INSERT INTO chtbuster.processtable(processID,processFileName,processName,processPath,hostName_id) VALUES (@processID, @fileName, @exeFile, @filePath, " + gethostname_id + ");";
                          string insert_ht = "INSERT INTO chtbuster.hostnametable(hostName_id) VALUES(@machineName);";
                          //query for else condition 1 is at the top
  
                          listBox1.ForeColor = System.Drawing.Color.Green;
                          listBox1.Items.Add(machineName + " has opened " + exeFile);
                          conDataBase.Open();
  
                          MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase);
                          cmd1Database.ExecuteNonQuery(); 
  
                          Int32 ifexist = (Int32)cmd1Database.ExecuteScalar();
                          if(ifexist&gt;0)
                          {
                             MySqlCommand cmd2Database = new MySqlCommand(gethostname_id, conDataBase);
                              machineName = (string)cmd1Database.ExecuteScalar(); 
                              cmd2Database.Parameters.AddWithValue("?machineName", machineName);
  
                              MySqlCommand cmd3Database = new MySqlCommand(insert_pt, conDataBase);
  
                                try
                                {
                                    cmd3Database.Parameters.Clear();
                                    cmd3Database.Parameters.AddWithValue("@processID", processID);
                                    cmd3Database.Parameters.AddWithValue("@machineName", machineName);
                                    cmd3Database.Parameters.AddWithValue("@filename", fileName);
                                    cmd3Database.Parameters.AddWithValue("@exeFile", exeFile);
                                    cmd3Database.Parameters.AddWithValue("@filePath", filePath);
                                    cmd3Database.ExecuteNonQuery();
                                    cmd2Database.ExecuteReader();
  
                                 }
                                catch (Exception ex)
                                {
                                    output.Text += "\n1" + (ex.Message);
                                } 
                          }
  
                          else
                          {
                             MySqlCommand cmd4Database = new MySqlCommand(Query, conDataBase);
                             MySqlCommand cmd5Database = new MySqlCommand(insert_ht, conDataBase);
  
                              try
                                {
                                    cmd5Database.Parameters.Clear();
                                    cmd5Database.Parameters.AddWithValue("@processID", processID);
                                    cmd5Database.Parameters.AddWithValue("@machineName", machineName);
                                    cmd5Database.Parameters.AddWithValue("@filename", fileName);
                                    cmd5Database.Parameters.AddWithValue("@exeFile", exeFile);
                                    cmd5Database.Parameters.AddWithValue("@filePath", filePath);
                                    cmd4Database.Parameters.AddWithValue("@machineName", machineName);
                                    cmd4Database.ExecuteNonQuery();
                                    cmd5Database.ExecuteNonQuery();
                                    conDataBase.Close();
  
                                 }
                              catch (Exception ex)
                              {
                                  output.Text += "\n2" + (ex.Message);
                              } 
                        }    
                      }
  
                  }
                  catch (Exception ex)
                  {
                      output.Text += "\n3" + (ex.Message);
                  } 
              }
  </code></pre>