Draconov
Draconov

Reputation: 1

I have a problem with my MySql 'SELECT' command

I want to get the first id from my table and write it at label16.text to create a pages for my program.Why the following code returns system.threading.tasks.unwrappromise'1[system.object]?

await sqlConnection.OpenAsync();               
SqlCommand command = new SqlCommand("SELECT Id from Recipes order by id asc limit 1", sqlConnection);
label16.Text = command.ExecuteScalarAsync().Result.ToString();

Upvotes: 0

Views: 98

Answers (1)

Giorgi Anakidze
Giorgi Anakidze

Reputation: 216

You should change

label16.Text = command.ExecuteScalarAsync().Result.ToString();

To

var t = await command.ExecuteScalarAsync();
label16.Text = t.Unwrap().ToString();

And to fullfill the answer, I'll paste this from comments too.

Another thing you have to do, is fixing your SQL query, it should be

SELECT TOP(1) Id from Recipes order by id asc

Third question

I'd do that this way

try 
{
    var id = int.Parse(label16.Text);
    sqlDataAdapter = new SqlDataAdapter($"SELECT * FROM Recipes WHERE Id = {id}", sqlConnection);
} catch { // error handling } 

Upvotes: 1

Related Questions