Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day!

Whenever I run my code, it would return this particular error "MySqlException was unhandled by user code: Fatal error encountered during command execution" which is pointed on
C#
long count = (long)cmd1Database.ExecuteScalar();


What could be the problem here? I am totally new to c#. Any help would be much appreciated.

C#
string constring = "Database=chtbuster;Data Source=localhost;User Id=root;Password='';";
                    string count1 = "SELECT COUNT(hostName) FROM chtbuster.hostnametable WHERE hostName == @machineName; ";
                    using (MySqlConnection conDataBase = new MySqlConnection(constring))
                    {
                        conDataBase.Open();

                        string insertprocess = "INSERT INTO chtbuster.processtable(processID,processFileName,processName,processPath,hostName) VALUES (@processID, @fileName, @exeFile, @filePath, @machineName);";
                        MySqlCommand cmd2Database = new MySqlCommand(insertprocess, conDataBase);

                        cmd2Database.Parameters.AddWithValue("@processID", processID);
                        cmd2Database.Parameters.AddWithValue("@filename", fileName);
                        cmd2Database.Parameters.AddWithValue("@exeFile", exeFile);
                        cmd2Database.Parameters.AddWithValue("@filePath", filePath);
                        cmd2Database.Parameters.AddWithValue("@machineName", machineName);
                        cmd2Database.ExecuteNonQuery();

                        //string checkname = "SELECT hostName FROM chtbuster.hostnametable WHERE hostName == @machineName; ";
                        MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase);
                        long count = (long)cmd1Database.ExecuteScalar();
                        if (count == 1)
                        {
                            listBox1.Items.Add(new MyListBoxItem(Color.Gold, count + "st warning" + machineName + " has opened " + fileName + " from path " + filePath));
                            if(machineName == pc1.Text)
                            {
                                pc1.Image = Image.FromFile("../../firstwarning.png");
                            }
                            else if (machineName == pc2.Text)
                            {
                                pc2.Image = Image.FromFile("../../firstwarning.png");
                            }
                            else
                            {
                                pc3.Image = Image.FromFile("../../firstwarning.png");
                            }
                           
                        }
                        else if (count == 2)
                        {
                            listBox1.Items.Add(new MyListBoxItem(Color.Orange, count + "nd warning" + machineName + " has opened " + fileName + " from path " + filePath));
                            if (machineName == pc1.Text)
                            {
                                pc1.Image = Image.FromFile("../../secondwarning.png");
                            }
                            else if (machineName == pc2.Text)
                            {
                                pc2.Image = Image.FromFile("../../secondwarning.png");
                            }
                            else
                            {
                                pc3.Image = Image.FromFile("../../secondwarning.png");
                            }
                        }
                        else if (count == 3)
                        {
                            listBox1.Items.Add(new MyListBoxItem(Color.Firebrick, count + "rd warning" + machineName + " has opened " + fileName + " from path " + filePath));
                            if (machineName == pc1.Text)
                            {
                                pc1.Image = Image.FromFile("../../thirdwarning.png");
                            }
                            else if (machineName == pc2.Text)
                            {
                                pc2.Image = Image.FromFile("../../thirdwarning.png");
                            }
                            else
                            {
                                pc3.Image = Image.FromFile("../../thirdwarning.png");
                            }
                        }
                        else { listBox1.Items.Add(new MyListBoxItem(Color.Gray, count + "th warning" + machineName + " has opened " + inputfile + " from path " + filePath));  }
                        conDataBase.Close();
Posted
Updated 10-Oct-15 5:36am
v2

1 solution

MySQL doesn;t use "==" as an equality operator: it uses the SQL standard "=" instead.
https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html[^]
So your command:
C#
string count1 = "SELECT COUNT(hostName) FROM chtbuster.hostnametable WHERE hostName == @machineName; ";
Is going to error.
 
Share this answer
 
Comments
Member 12046490 10-Oct-15 11:29am    
Thanks for the quick reply sir. I have tried removing the unnecessary '=' but I am still receiving the same error.
OriginalGriff 10-Oct-15 11:32am    
OK - so what line is occurring on, and what is the inner exception saying?
Member 12046490 10-Oct-15 11:37am    
It would give me this particular error "MySqlException was unhandled by user code: Fatal error encountered during command execution" pointing at long count = (long)cmd1Database.ExecuteScalar();
OriginalGriff 10-Oct-15 11:43am    
You haven't provided the parameter value "@machineName"
Copy this line:
cmd2Database.Parameters.AddWithValue("@machineName", machineName);
Change "cmd2Database" to "cmd1Database" and add it just above the line giving the error.
Member 12046490 10-Oct-15 11:47am    
ah thank you so much sir! Got it all working now!

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