Reputation: 87
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 at
long count = (long)cmd1Database.ExecuteScalar();
What could be the problem here? I am totally new to c#. Any help would be much appreciated.
string constring = "Database=chtbuster;Data Source=localhost;User Id=root;Password='';";
string count1 = "SELECT COUNT(hostName) FROM chtbuster.processtable 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();
Upvotes: 0
Views: 2488
Reputation: 216363
You have forgot to add the parameter required by the cmd1Database command
----
string count1 = @"SELECT COUNT(hostName) FROM chtbuster.processtable
WHERE hostName = @machineName; ";
MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase);
cmd1Database.Parameters.AddWithValue("@machineName", machineName);
long count = (long)cmd1Database.ExecuteScalar();
----
Upvotes: 1