Tjazz Tjazz - 2 months ago 4
MySQL Question

Update value database mysql using c#

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 retun a value.

Please dont say that there is alot to find on the internet, because of my lack of knowledge and the fact that i am new, i am not able to understand what they mean. Thanks in advance!

Answer

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;
   }
Comments