I need to execute the following command and pass the result to a label. I don’t know how can i do it using Reader. Someone can give me a hand?
String sql = "SELECT * FROM learer WHERE learer.id = " + index;
SqlCommand cmd = new SqlCommand(sql,conn);
learerLabel.Text = (String) cmd.ExecuteReader();
As you can see i create the SQL statement and i execute it, but it does not work. Why?
The console says:
Cannot implicitly SqlDataReader to
String…
How can i get the desired results as String so the label can display it properly.
It is not recommended to use
DataReaderandCommand.ExecuteReaderto get just one value from the database. Instead, you should useCommand.ExecuteScalaras following:Here is more information about Connecting to database and managing data.