4

I am trying to execute a INSERT statement on a mySQL DB in C#:

MySqlConnection connection = new MySqlConnection("SERVER=" + _dbConnection + ";" +
   "DATABASE=" + _dbName + ";" +
   "PORT=" + _dbPort + ";" +
   "UID=" + _dbUsername + ";" +
   "PASSWORD=" + _dbPassword + ";");
MySqlDataAdapter adapter;
DataSet dataset = new DataSet();
command = new MySqlCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO plugins (pluginName, enabled) VALUES (@plugin,@enabled)";
command.Parameters.AddWithValue("@name", "pluginName");
command.Parameters.AddWithValue("@enabled", false);
adapter = new MySqlDataAdapter(command);
adapter.Fill(dataset);

The plugin table consists of two columns: pluginName(varchar(50)) and enabled(boolean).

This fails with the error: mysql Fatal error encountered during command execution. Any ideas on why this would fail?

Brian
  • 1,397
  • 9
  • 33
  • 51

4 Answers4

7

You seem to be using @plugin in the query but @name in the parameters, although I'd expect a better error message if that was the sole problem!

Chris
  • 10,337
  • 1
  • 38
  • 46
  • 3
    If you explore the Exception's InnerException (its exceptions all the way...) you eventually find the source of the problem; in my case, a non-defined parameter (typo). – fableal May 27 '13 at 13:43
  • I just had this same problem, the error message is not clear at all. But thank you for helping me fix it! – AdamMc331 May 04 '15 at 19:27
0

The error "Fatal error encountered during command execution" seems to be for general syntax errors in the CommandText line.

For example, the following just tripped me up for a while:

...@date,@email@first,@last...

Watch out for those commas! There should be one after @email:

...@date,@email,@first,@last...
DanM7
  • 2,203
  • 3
  • 28
  • 46
0

Missing Parameters could also be a reason. eg:

cmd.CommandText = "INSERT INTO login VALUES(@uname,@pwd);"
cmd.Parameters.AddWithValue("@uname",TxtUsername.Text.ToString())
'forgot to place "@pwd"

cmd.ExecuteNonQuery()

You get fatal error which can be solved by adding

cmd.Parameters.AddWithValue("@pwd",TxtPassword.Text.ToString())

0
`(from user in _context.GetDataTable("call sp_get_user_subscribe_info(@param1, @param2)", _context.AddParameter(new dynamic[] { id }))`

My procedure has two parameters but I have passed only one parameter that's why this error message occurred, But this error message is so confusing.