-1

I use this method to increase value of logins column by one:

MySqlConnection connect = new MySqlConnection(connectionStringMySql);
MySqlCommand cmd = new MySqlCommand();

cmd.Connection = connect;
cmd.Connection.Open();

string commandLine = null;
commandLine = "UPDATE Users SET logins = (@cur_value := logins) + 1 WHERE id = @id;";

cmd.CommandText = commandLine;


cmd.Parameters.AddWithValue("@id", userId);

cmd.ExecuteNonQuery();
cmd.Connection.Close();

And i get this Exception:

Fatal error encountered during command execution.

2
  • 1
    What about @cur_value? You didn't set parameter for it. Commented Apr 1, 2013 at 17:55
  • 1
    You aren't supplying the value of @cur_value, maybe that's it? Commented Apr 1, 2013 at 17:55

3 Answers 3

1

Just try this code

string commmandLine = "UPDATE Users SET logins = logins + 1 WHERE id = @id";
cmd.Set
Sign up to request clarification or add additional context in comments.

Comments

0

First try this code

commandLine = "UPDATE Users SET logins =1 WHERE id = @id";

If above code is does not threw any error, then try below code

commandLine = "UPDATE Users SET **logins =@YourValue** WHERE id = @id";

cmd.CommandText = commandLine;
cmd.Parameters.AddWithValue("@id", userId);

cmd.Parameters.AddWithValue("@YourValue", 1);//**Put your logins value**

Comments

-1

Add following line:

 cmd.Parameters.AddWithValue("@cur_value", yourValue);

Also make more clear sql statement:

UPDATE Users 
SET logins = @cur_value + 1 
WHERE id = @id;

It is good practice to enclose problematic pieces of code with try/catch. Classes which represents unmanaged resources like Stream or SqlConnection are implementing IDisposable interface. They are usually need to be enclosed with using statement to be able to correctly release their resources even if exception was thrown (or use try/finally and in finally block call to method Close or Dispose). Then your code will be nice and clear :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.