0

Attempting to execute a block of code on a button click to retrieve data from a SQL database. During build, I get the error message

ItemsPage.Button_Clicked(): not all code paths return a value

I tried adding a return statement with a value of the int points but it returned the error

Cannot implicitly convert type int to System.EventHandler

I'm a C# beginner so have little idea on what to do even after attempting to find other solutions online.

public async Task<EventHandler> Button_Clicked()
{
    using (MySqlConnection connection = new MySqlConnection("server=localhost;user=app;database=travel_logger;port=3306;password=app"))
    {
        connection.Open();

        var cmd = new MySqlCommand("SELECT points FROM * WHERE email= 20Test@test");
        var reader = await cmd.ExecuteReaderAsync();
        int points = (int)Convert.ToInt64(reader.GetInt64(0));
        return points;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jsheerin
  • 35
  • 4
  • 2
    Your code returns an `int` and the method declares the returned value to be a `Task`. – mcjmzn Jun 26 '22 at 19:07
  • 1
    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/async-return-types – Mihal By Jun 26 '22 at 19:09
  • Why does your method return `Task` and not just `Task`? – Kirk Woll Jun 26 '22 at 19:34
  • `Task` indicates that it expects an object of type `EventHandler` to be returned. `Task` indicates that it expects an integer to be returned. Personally I suggest you just use `Task` and not return (I don't see it necessary in a Button Click event) – Héctor M. Jun 26 '22 at 19:59
  • Suggested reading: [C# compiler error: "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-compiler-error-not-all-code-paths-return-a-value) – Ňɏssa Pøngjǣrdenlarp Jun 26 '22 at 20:10
  • May I say that there's something weird here. You're using Xamarin forms but connecting to a local MySQL database. That would make some sense on Windows, but not on iOS and Android. – Ian Newson Jun 26 '22 at 20:48

1 Answers1

1

Task<EventHandler> indicates that it expects an object of type EventHandler to be returned. Task<int> indicates that it expects an integer to be returned. Personally I suggest you just use Task (equivalent to void but supports await) or async void and not return (I don't see it necessary in a Button Click event)

I understand that you could opt for a more convenient structure:

private async Task<int> GetPoints()
{
    using (var conn = new MySqlConnection("server=localhost;user=app;database=travel_logger;port=3306;password=app"))
    {
        connection.Open();

        var cmd    = new MySqlCommand("SELECT points FROM * WHERE email= 20Test@test");
        var reader = await cmd.ExecuteReaderAsync();
        int points = (int)Convert.ToInt64(reader.GetInt64(0));
        return points;
    }
}

Then:

public async void Button_Clicked()
{
    int points = await GetPoints();
}
Héctor M.
  • 2,302
  • 4
  • 17
  • 35