0

I want to update "todo" in table user, where the value "username" is the same as the value in my code _naam. What I have so far (kind of copied from my "read" command):

private bool Todo_updaten(string _todo)
    {
        db_connection();
        MySqlCommand cmdUpdate = new MySqlCommand();
        cmdUpdate.CommandText = "UPDATE `user` SET `todo`=@todo WHERE `username` LIKE '" + _naam + "'";
        cmdUpdate.Parameters.AddWithValue("@todo", _todo);
        cmdUpdate.Connection = connect;
        MySqlDataReader tbupdaten = cmdUpdate.ExecuteReader();
        if (tbupdaten.Read())
        {
            tbTodo.Text = tbupdaten.GetString(0);
            connect.Clone();
            return true;
        }

I get an error at my bool todo_updaten, which says: not all code paths return a value.

2
  • 1
    Add another return at method's end. false, I suppose, given your logic. Yours only returns if the code enters the block, so it's not lying to you. Commented Sep 22, 2016 at 10:31
  • 1
    Your return true statement is inside your if (tbupdaten.Read()). Be sure to return something outside as well. e.g. else { return false; } Commented Sep 22, 2016 at 10:31

2 Answers 2

1

Because the return type of your Todo_updaten function is bool so you always need to return any bool value from it. At present, you are returning true only if the condition is fulfilled but if not then you are not returning anything. Which is wrong so you can return false in else statement to overcome it, or any of your custom logic. But make sure you are returning a bool value from the function always.

if (tbupdaten.Read())
   {
       tbTodo.Text = tbupdaten.GetString(0);
       connect.Clone();
       return true;
   }
   else
   {
       return false;
   }
Sign up to request clarification or add additional context in comments.

Comments

1

Your function does not return anything if tbupdaten.Read() returns false, because you only have a return in that if block.

Also when updating you database you should call ExecuteNonQuery instead of ExecuteReader.

private bool Todo_updaten(string _todo)
{
  db_connection();
  MySqlCommand cmdUpdate = new MySqlCommand();
  cmdUpdate.CommandText = "UPDATE `user` SET `todo`=@todo WHERE `username` LIKE @naam";
  cmdUpdate.Parameters.AddWithValue("@todo", _todo);
  cmdUpdate.Parameters.AddWithValue("@naam", _naam);
  cmdUpdate.Connection = connect;
  cmdUpdate.ExecuteNonQuery();
  return true;       
} 

The ExecuteNonQuery also returns the number of rows affected if you want to check anything.

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.