The code lacks not only the setting of the query result but also other parts necessary to execute the query.
private void button5_Click(object sender, EventArgs e)
{
connect.Open();
string avg= "SELECT AVG(number) FROM info WHERE number = @number";
SqlCommand command= new SqlCommand(avg, connect);
// You need to add a parameter for @number and set its value
command.Parameters.Add(@number, SqlDbType.Int).Value = valueForNumber;
// Now you need to execute the command to get back the average value
object result = command.ExecuteScalar();
// Finally you set the textbox with a conversion in string of the returned value
// but also check if there is really a return value
if(result != null)
textBox8.Text = result.ToString();
else
textBox8.Text = "Number Not found!";
connect.Close();
}
But there are other problems with that code.
When you deal with Disposable objects you should have a red flag in mind. Disposable objects "usually" contains resources that should be freed as soon as possible.
The connection and the command are this kind of objects. In the original code it seems that the connection is a global object. This should be avoided with code like this
private void button5_Click(object sender, EventArgs e)
{
using(SqlConnection cnn = new SqlConnection(getTheConnectionString())
{
connect.Open();
string avg= "SELECT AVG(number) FROM info WHERE number = @number";
using(SqlCommand command= new SqlCommand(avg, cnn))
{
// You need to add a parameter for @number and set its value
command.Parameters.Add("@number", SqlDbType.Int).Value = valueForNumber;
// Now you need to execute the command to get back the average value
object result = command.ExecuteScalar();
// Finally you set the textbox with a conversion in string of the returned value
// but also check if there is really a return value
if(result != null)
textBox8.Text = result.ToString();
else
textBox8.Text = "Number Not found!";
} // End using => SqlCommand disposed...
} // End using => SqlConnection closed and disposed
}
The using blocks keep track of disposable objects and ensure that a proper dispose of these objects happens when the code exits the using block even if an exception occurs inside the blocks.